Programming in C



  • In C, the program to print ``hello, world'' is


  •    #include 
       main()
       {
         printf("hello, world\n");
       }
    

  • On the UNIX operating system you must create the program in a file whose name ends in `` .c '', such as hello.c , then compile it with the command cc hello.c


  • The compilation will proceed silently, and make an executable file called a.out . If you run a.out by typing the command a.out



Structure of a C Program


  • A C program, whatever its size, consists of functions and variables .


  • A function contains statements that specify the computing operations to be done, and variables store values used during the computation.


  • In our example is a function named main . Normally you are at liberty to give functions whatever names you like, but `` main '' is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere.


  • A method of communicating data between functions is for the calling function to provide a list of values, called arguments , to the function it calls. The parentheses after the function name surround the argument list.


  • The statements of a function are enclosed in braces { } .




Variables and Arithmetic Expressions


  •    
    #include 
       /* print Fahrenheit-Celsius table
           for fahr = 0, 20, ..., 300 */
       main()
       {
         int fahr, celsius;
         int lower, upper, step;
         lower = 0;      
         upper = 300;   
         step = 20;     
         fahr = lower;
         while (fahr <= upper) {
             celsius = 5 * (fahr-32) / 9;
             printf("%d\t%d\n", fahr, celsius);
             fahr = fahr + step;
         }
       }

  • The program consists of the definition of a single function named main . It is longer than the one that printed `` hello, world" and introduces several new ideas, including comments, declarations, variables, arithmetic expressions, loops , and formatted output.


  • Any characters between /* and */ are ignored by the compiler; they may be used freely to make a program easier to understand. Comments may appear anywhere where a blank, tab or newline can


  • n C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements.


  • A declaration announces the properties of variables; it consists of a name and a list of variables, such as


    1. int fahr, celsius;


    2. int lower, upper, step;


  • The type int means that the variables listed are integers; by contrast with float , which means floating point, i.e., numbers that may have a fractional part.


  • The range of both int and float depends on the machine you are using; 16-bits ints, which lie between -32768 and +32767, are common, as are 32-bit ints.


  • A float number is typically a 32-bit quantity, with at least six significant digits and magnitude generally between about 10-38 and 1038.


  • C provides several other data types (The size of these objects is also machine-dependent.) :


    1. char - "character" a single byte


    2. short - short integer


    3. long - long integer


    4. double - "double-precision" floating point



Information about "printf"


  • printf is a general-purpose output formatting function


  • Its first argument is a string of characters to be printed, with each % indicating where one of the other (second, third, ...) arguments is to be substituted, and in what form it is to be printed.


  • For instance, %d specifies an integer argument, so the statement printf("%d\t%d\n", fahr, celsius); causes the values of the two integers fahr and celsius to be printed, with a tab ( \t ) between them.


  • Each % construction in the first argument of printf is paired with the corresponding second argument, third argument, etc.


  • If we augment each %d in the printf statement with a width, the numbers printed will be right-justified in their fields. We use printf("%3d %6d\n", fahr, celsius); to print the first number of each line in a field three digits wide, and the second in a field six digits wide


  • Examples of using printf :


    1. %d - print as decimal integer


    2. %6d - print as decimal integer, at least 6 characters wide


    3. %f - print as floating point


    4. %6f - print as floating point, at least 6 characters wide


    5. %.2f - print as floating point, 2 characters after decimal point


    6. %6.2f - print as floating point, at least 6 wide and 2 after decimal point


    7. %o for octal, %x for hexadecimal, %c for character, %s for character string and %% for itself.




The for statement


  • #include 
       /* print Fahrenheit-Celsius table */
       main()
       {
           int fahr;
           for (fahr = 0; fahr <= 300; fahr = fahr + 20)
               printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
       }

  • The for statement is a loop, a generalization of the while. Within the parentheses, there are three parts, separated by semicolons.


  • The first part, the initialization is done once, before the loop proper is entered :


    1. fahr = 0

  • The second part is the test or condition that controls the loop. This condition is evaluated; if it is true, the body of the loop is executed :


    1. fahr <= 300

  • Then the increment step is executed, and the condition re-evaluated. The loop terminates if the condition has become false :


    1. fahr = fahr + 20


  • As with the while, the body of the loop can be a single statement or a group of statements enclosed in braces. The initialization, condition and increment can be any expressions.





Symbolic Constants



  • The quantities LOWER, UPPER and STEP are symbolic constants, not variables, so they do not appear in declarations.


  • #include 
       #define LOWER  0     /* lower limit of table */
       #define UPPER  300   /* upper limit */
       #define STEP   20    /* step size */
       /* print Fahrenheit-Celsius table */
       main()
       {
           int fahr;
           for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
               printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
       }

  • Symbolic constant names are conventionally written in upper case so they can ber readily distinguished from lower case variable names.


  • There is no semicolon at the end of a #define line.





Character Input and Output



  • Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters.


  • The standard library provides several functions for reading or writing one character at a time such as getchar and putchar.


    1. getchar() :getchar reads the next input character from a text stream and returns that as its value i.e. c = getchar();


    2. putchar() : prints the contents of the integer variable "c" as a character.


  • #include 
       /* copy input to output; 1st version  */
       main()
       {
           int c;
           c = getchar();
           while (c != EOF) {
               putchar(c);
               c = getchar();
           }
       }

  • getchar returns a distinctive value when there is no more input. This value is called EOF, for ``end of file". To store this we can't use char since "c" must be big enough to hold EOF in addition to any possible char. Therefore we use int.