C Programming Language

By Jorge Ramirez

 

Program Forms

Statements

Variable types

Storage Classes

Declaring Variables

 

 

 

 

Program Forms

A program consists of one or more functions. One must be named main(), and execution starts with it. The statements of a function are enclosed between opening and closing braces:

 

Main()    /* function name*/

{             /* opening brace */

    int m; /* declaration statement */

  

   for ( m = 1; m < 5; m++)

   printf(“%d %d\n”, m, m + 1);

}           /* closing brace*/

 

Programs may include preprocessor directives and more than one function. Function definitions follow one another. Do not embed one function definition within another function.  Execution starts at main() regardless of the function order. Functions should declared, or prototyped, before they are used.

 

#define RANGE 15 /* preprocessor definition */

int odd(int);              /* ANSI prototype for odd() */

void main (void)     /* specify type, arguments */

{

     int n;

    

     for (n=1; n< RANGE; n++)

     printf(“%d %d\n”, n, odd(n));

}    /* end of main() function */

 

int odd(int k)  /*odd() has int arg and an */

{

   int j;  /* declare local variables */

 

   j=2*k+1;

   return j;

}   /* end of odd() function */

 

In a function with arguments, the arguments are declared before the body of the function, as marked by the opening brace, and local variables are declared after the brace.

 

Statements

Simple Statements consists of an expression or instruction followed by a semicolon:

 

float x;   /* declaration statement */

x=1.14;  /* assignment statement */

printf(“Friends!”); /* function call statement */

 

Structured Statements consist of a keyword followed with a test statement within parentheses followed by simple statement.

 

If (variable == 12345)

   y=m*x+b;

 

A compound statement, or block, consists of one or more statements enclosed in braces. It counts as one statement and is used in structured statements to allow the inclusion of more than one action in the statement:

 

If (idnumber == 10007)

{

     salary = salary + bonus;

     status++;

}

Variable Types

Simple variables come in two basic varieties: integer and floating point.

 

Integer types

 

 

char

signed char

unsigned char

short

int

long

unsigned, unsigned int

unsigned short

unsigned long

 

 

 

Floating types

 

float

double

long double

 

Storage Classes

A variable belongs to one of five storage classes.

 

Storage Class

Keyword

Duration

Scope

 

 

 

 

Automatic

Auto

Temporary

Local

 

Register

Register

Temporary

Local

 

Static

Static

Persistent

Local

******************************************************************

External

Extern

Persistent

Global (all files)

External static

Static

Persistent

Global (one file)

 

Declaring Variables

Simple Statements consists of an expression or instruction followed by a semicolon:

 

float x;   /* declaration statement */

x=1.14;  /* assignment statement */

printf(“Friends!”); /* function call statement */h