Structure Of C Program

Basic Structure Of “C” Program

  • Documentation section
  • Definition section
  • Global declaration section
  • Main function section
  • Subprogram section

Documentation section

The documentation section consist of a set of comment lines giving

  • Name of the program
  • purpose of program
  • Name of the author / programmer
  • Date of programming
  • Details about calculations performed in the program.
  • Variable details if required

Comments can be given in two ways

(i) Multiple line comment statements

          /* 

statements                  

statements 

*/

or

/* statements  */

Note: works in both “c” and “c++” compiler

(ii) Single line comment statement

// statement

//statement

//statement

Note: works in only “C++” compiler

Definition section

In this section all symbolic constants are declared using #define statements

Example:

#define a 10

#define pi 3.1415

This section is optional

Global declaration section

There are some variables that are used in more than one function. Such variables are called global variables and are declared in global declaration section that is outside all the functions.

This section also declares all the user defined functions

Main function section

  • Every “C” program must have one main() function section. This is the function from where the execution of the program starts.
  • These sections consist of two parts, declaration part and executable part. The declaration part declares all the variables used in the executable part.
  • These two parts appear between the {} (opening and closing braces)
  • The program execution begin at the opening braces and ends at the closing braces.
  • The closing brace of the main() function is the logical end of the program.

Subprogram section

  • Subprogram section contains all the user defined functions that are called in the main() function or some other functions.
  • User defined functions are generally placed immediately after the main function. ( These may some times be given above the function main).
  • The definition of the function can appear in any order.