Total Pageviews

Monday, 30 April 2012

C Program Structure


C Program Structure
A C program basically has the following form:
  • Preprocessor Commands
  • Type definitions
  • Function prototypes -- declare function types and variables passed to function.
  • Variables
  • Functions
 
We must have a main() function.

A function has the form:

type function_name (parameters)
              {
                             local variables

                             C Statements

               }
If the type definition is omitted C assumes that function returns an integer type. NOTE: This can be a source of problems in a program.

So returning to our first C program:
  /* Sample program */

              main()
                             {

                             printf( ``I Like C \n'' );
                             exit ( 0 );

                             }

NOTE:
  • C requires a semicolon at the end of every statement.
  • printf is a standard C function -- called from main.
  • \n signifies newline. Formatted output -- more later.
  • exit() is also a standard function that causes the program to terminate. Strictly speaking it is not needed here as it is the last line of main() and the program will terminate anyway.




Let us look at another printing statement:

printf(``.
\n.1\n..2\n...3\n'');

The output of this would be:

              .
              .1
              ..2
              ...3

The C Compilation Model
We will briefly highlight key features of the C Compilation model (Fig. 2.1) here.

                                                                               Source Code

 
                                                                               



                The C Compilation Model

No comments:

Post a Comment