22 Kasım 2017 Çarşamba

Learning C Programming Step by Step

STEP#0: Download, Install and Run a Compiler

  1. Go to http://www.codeblocks.org/
  2. Click on "Download" -> "Download the binary release" -> "Windows XP / Vista / 7 / 8.x / 10" there in order.
  3. Download "codeblocks-16.01mingw-setup.exe" by clicking on "Sourceforge.net" on the same line.
  4. Install the downloaded program on your computer. (Use the default option during installation.)
  5. Create a new folder -named "New_C_Projects" for instance- on your desktop screen in order to be used from now on for your new projects.
  6. There will be a desktop icon on the screen -with name "CodeBlocks"- after the installation. A double-click on this icon will run the program. Make a double-click on the icon "CodeBlocks" in order to run the program.
  7. Click on "Create a new project" and choose "Console Application".
  8. Click "Next", pick "C", click "Next", and choose the new folder that you have created at "5." as "Folder to create project in:".
  9. Write a project title without a gap between the words, for example "Step0". Click on "Next" and then "Finish".
  10. On the left of the screen, you can see a control panel with segments "Projects", "Symbols", ... etc. Under "Projects", you can see the name of your new project title created at "9.". Below this title, you can see "Sources", click on the "+" sign next to this title. Make a double-click on "main.c".
  11. In the control panel, you can see lots of buttons to click on. You can use "Build" and "Run", or only "Build and Run" to run the code on the screen.

STEP#1: Introduction

A computer program needs functions to run a code. Whenever you run a program, the computer look for a function called "main" automatically. So you need this "main" function no matter what.
e.g. 
#include <stdio.h> 
#include <stdlib.h>
int main()
{   
printf("Hello world!\n");                              /* It will print "Hello world!" on the screen */
return 0;
}
 Be careful about the followings:
  • Do not forget to close every paranthesis that you have opened earlier.
  • End every line in the function with a ";" sign.
  • You can put comments or reminders for yourself in your code whenever you want by using one the followings: 
  • For multiple lines use "/*...............................*/".
  • For a single line use "// .............................. " or "/*...............................*/".

STEP#2: Printing Something on a Screen

1. You can use the function "printf()" as follows:
  • Use the marks " " to write a text.
  • Use "\n" for a new line.
  • Use "\t" for a tab space.
  • Use "\a" for an alert sound after the print.
e.g.  
printf("PINAR is trying to write a tutorial for C Programming!\n");  
2. You may prefer to use the same "printf()" function as follows: "printf("%s ....", "");" In this case;
  • "%s"  is a string of characters which is not defined at the very beginning, but it will be substituted with the words right after the sign "," on the same line.
e.g.  
printf("%s is trying to write a tutorial for C Programming!\n", "PINAR");  
You can use multiple "%s" as follows:
    e.g.  
    printf("%s is trying to %s a tutorial for C Programming!\n", "PINAR", "write");  
    3. If you want to write a number (which is an integer) instead of a text (or string characters), you need to use "%d" instead of "%s" as follows:
    e.g.  
    printf("%d children are playing together.\n", 3);  
     4. Similarly, for a floating number (which is not an integer), you need "%f". However, you should be careful about that "%f" allows you to display only 6 decimals. (i.e. The below command prints "The answer is 1.508375." on the screen.)
    e.g.  
    printf("The answer is %f.\n", 1.508375483753223);  
    5. If you want to round the number given to a number with less decimals (say, you want to display a number with 2 decimals instead of 6 decimals), you can use "%f" as follows:
    e.g.  
    printf("The answer is %.2f.\n", 1.508375483753223);  

    STEP#3: Integer Variables

    1. Notice that
    • Variables cannot start with numbers! (i.e. "86pinar" cannot be a variable, but "pinar86" can be.)
    • Variables cannot have spaces in it! Use the marks " " to write a text. (i.e. "pinar the girl" cannot be a variable, but "pinar_the_girl" can be.)
    • Variables cannot have weird symbols in it -such as *, $, #, ... (i.e. "pinar*the*girl" cannot be a variable.) 
    2. While creating a variable, first of all, you need to give the type of this variable. It may be one of the followings:
    • int (if the variable is an integer)
    • char (if the variable is a character array)
    3. In order to store a data in an int varible, you need to use "=" sign as follows:
    e.g.  
    int x; 
    x=283;  
    or  
    int x =283;  
    And if you want to print this stored data in x, you can use the function "printf()" as follows:
    e.g.  
    printf("The integer x is %d", x); 

    STEP#4: Character Arrays

    1. In order to store a data in a char varible of length n, you need to use "=" sign as follows:

    e.g.  
    char x[6]; 
    x="pinar";  
    or  
    char x[6] ="pinar";  
    !!! Here, you should be careful about this fact: Although your string consists of 5 letters, your character varible needs 6 places (coming from 5 places for letters + 1 place for "\0" denoting the end of any string)

    In fact, if you are assigning a data in a char varible, you can also do it without defining the length of the character array in the beginning as below. However, if you are planning to do it so, you need to do it in just one line. 
    e.g.  
    char y[] ="pinar";  
    2. If you want to print this stored data in x, you can use the function "printf()" as follows:
    e.g.  
    printf("My name is %c", x);  
    3. In fact; a character array of lenght n is something like a sequence of letters { x_0, x_1, x_2, ..., x_n }. You can think any letter of this character array as a member of a finite sequence. For instance, the output of the following code will be "r" which is the third letter.
    e.g.  
    char x[6]="pinar";
    printf("The second letter of my name is %c", x[2]);
    You can change any element in your character array. For instance, the output of the following code will be "pidar", not "pinar".
    e.g.  
    char x[6]="pinar";
    x[2]='d';
    printf("My name is %c", x);
    4. If you want to assign a new string to your array, you should use the function "strcpy()" as follows:
    e.g.  
    char x[]="pinar";
    printf("My name is %c.\n", x); 
    strcpy(x, "Miranda");
    printf("My name is %c.\n", x);
    The output of the above code will consists of two lines: "My name is pinar." and then "My name is Miranda." As you can here, the length of these characters may not be the same. (Beware that the length of the character array hasn't been given in the very begining of this example.)

    STEP#5: Getting Input from the User                                                                                                                                                          

    1. In order to get a string of characters from the user, you can use the function "scanf()" as follows:
    e.g.  
    int main()
    {   
    char name[20];
    printf("Please, type your name: ");
    scanf("%s", name);
    }
    2. In order to get an integer from the user, you can use the function "scanf()" as follows:
    e.g.  
    int main()
    {   
    int x;
    printf("How old are you?");
    scanf("%d", &x);                                                    \\Be careful about the sign "&" here!!!
    }
    STEP#6: Mathematical Operators                                                                                                                                                              

    Here, the list of facts about the mathematical operators:
    • You can work in modulus as follows: 
    e.g.   
    int x, y, r; 
    x = 543;
    y = 18; 
    r = x%y;                                                                  \\ It means "r = x mod y"
    printf("If you divide 543 by 18, the remainder will be %d", r);
     2. Be careful, scanning a value as a int or float may change the answer.
    e.g. The following code will get the output 30.
    int x, y, z; 
    x = 543;
    y = 18; 
    z = x/y;                                                                
    printf("If you divide 543 by 18, you will obtain the result %d", z);     
    e.g. The following code will get the output 30.166666.
    float x, y, z; 
    x = 543;
    y = 18; 
    z = x/y;                                                                
    printf("If you divide 543 by 18, you will obtain the result %f", z);     

    STEP#7: Creating a Header File                                                                               

    1. There are two types of header files in fact: "The system header files" and "Header files of your own program".
    e.g.  Two system header files
    #include <stdio.h>                                    // Stands for "standard input and output".                       
    #include <stdlib.h>                                  // Stands for "standard library".
    If you want to insert a header file of your own program to your program, you may use the directive "#include" again. However, we will turn to this later because before inserting a header file, you need to create one.

    2.  In order to create a header file of your own, you need to click on "File" -> "New" -> "Empty File" -> "Yes" in order. Then you should giv a name to this header file, but it must have an extension ".h". (For instance; you can use the name "MyOwnInfo.h") Then click on "OK" button. Now, you can see the name of your header file

    • On the left of the screen below the title "Headers" by clicking on the "+" sign.
    • On the control panel above, at the right hand side of the source file "*main.c" there. 
    3. Next, you can put several data in the header file you've just created. (This file is empty in the very begining.)
    e.g.  All of the data in MyOwnInfo.h
    #define NAME "Pinar"                     
    #define SURNAME "Ongan" 
    #define YEAROFBIRTH 1986                      

    4. Now, you can insert your header file to your source code by using the directive "#include" as follows:
    e.g. 
    #include "MyOwnInfo.h"                         
    Be careful about the followings:
    • The directive "#include" in order to insert your header file must be just after the system header files and definitely before the main function.
    • The system header files are included with the command #include <...>, but the header files of your own program are included with the command #include "...."
    • Inserting a header file to your source file will lead all data in the header file to be copied to the source file. (This data stays as constants, they are not variables.