Selected C formats


/* enclose comments within slash-star and star-slash */


#include <stdio.h>                  Replace line with contents of system header file



int main( int argc, char *argv [] )                All programs start execution with the

                                                                                    function main.  argc contains the

                                                            number of command line arguments.  argv is an array

                                                            of pointers to those arguments.


char c;                                              Single byte-size variable.

char msg[]="HELP\n";                  Initialized array; 6 characters (last is 0).

double big;                                      Double-precision floating-point variable.

float matrix[10][50];                  Two-dimension array.

int i,j,k;                                        Three two-byte-sized variables.

long int sum;                            One four-byte-sized variables.

void myproc(void);                      Declare prototype function without a return value.


x = 1;                                                      Assignment expression.  Terminate simple statements

                                                            with semicolon.


;                                                                 It is legal to have a statement do nothing.


{ tmp=a; a=b; b=tmp; }           Surround compound statements with braces where a

                                                            simple statement would be allowed.


break;                                                   Terminate smallest enclosing do, for, switch or



continue;                                               Start the next cycle of a loop.


do c=getchar();  while (c==' ');   Perform statement until condition is false. 

                                                                        Always executes at least once.


for ( i=0; i<MAX; a[i++]=c )      Initialize once, then while condition is true do statement,

     c=getchar();                                   then do increment.  Initialization will occur, but

                                                            statement will not be executed if condition starts out



if (a<0) a = -a;                                Perform statement if condition is true.

else printf( "was plus\n" );    Optional else after if.


return;                                              Exit function with return type void.


return a+c;                                          Exit function and return value of optional expression to



switch (getchar()) {                        Evaluate expression and perform appropriate case.

     case 'X': exit(0);

     case 'H': help(); break;                          If no break, falls into next case.

     case 'A': case 'B': a++; break;               Multiple cases allowed.

     default: printf("Try again.\n");            Default if no case matched.



while (i<MAX) a[i++]=0;                     Perform statement while condition is true.  Statement

                                                            will not be executed if condition starts false.



Selected printf escape sequences


%%        percent sign

%c        single ascii character

%d        signed decimal integer (%ld for long)

%e            scientific notation with lower case e

%f        fixed point notation

%g        use %e or %f, whichever is shorter

%i        general integer conversion (%li for long)

%o            unsigned octal integer (%lo for long)

%s        string

%u            unsigned decimal integer (%lu for long)

%x            unsigned hexadecimal integer with lower case abcdef (%lx for long)



Precedence of Selected Operators


Primary expression

[] array element  () function call


Unary operators

& address  - minus  ! logical not   ++ inc  -- dec  sizeof  ()cast


Binary operators

* multiply  / divide  % modulus

+ add  - subtract

< less than  > greater than  <= less than or equal  >= greater than or equal

== equals  != not equals

&& logical and

|| logical or


Conditional expression

condition ? true : false


Assignment operators

=  *=  /=  %=  +=  -=