Structure of a C Program

C is a Structured Programming Language.

A C program can be viewed as a group of building blocks called functions. i.e. A C program is not more than a collection or set of functions. Each function is a collection of statements which performs a specific task in the program. or A function is a subroutine that may include one or more statements designed to perform as specific task. Write a C program, we first create functions and put them together.

A C program in general, consists of various sections as follows:

  • Documentation Section
  • Link Section
  • Definition Section
  • Global Declaration Section
  • Main() Function Section
  • Subprogram Section

explain the Structure of a C Program

Documentation Section :

To enhance the readability of the program, programmers can provide comments about the program in this section, the comments are included between the delimiters /* and */.

These statements are not executable rather they are ignored by the compiler, but too many comments should be avoided.

In C comments can not be nested. We can not write comments with in comments.

Example : /* program for printing a line of text */

Link Section :

The Link Section provides instructions to the compiler to link functions from the system library.

A C program depends on the header files to a great extent. A header file contains the information required by the compiler, which calls the library functions used by the program during compilation. A header file has an extension of .h. These files are included in the program using the preprocessor directory #include .

Example : #include <stdio.h>

stdio.h is the header file for the library function printf().

Definition Section :

The definition section defines all the symbolic constants.
Example : #define PI 3.14159

Global Declaration Section :

This section is used for declaring the global variables and also for declaring all the user defined functions.

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

Example : 1. int a, b, c;
2. int sum(int a, int b)

Main() Function Section :

Every C program must have one main() function section. This section contains two parts, declaration part and executable part.

The declaration part declares all the variables used in the executable part. There is at least one statement in the executable part.

These two parts must appear between the opening and the closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function section is the logical end of the program.
All statements in the declaration and executable parts end with a semicolon(;).

Subprogram Section :

C Language provides a facility for the users to define their own functions. The subprogram section contains all the user defined functions, that are called in the main function. User defined functions are generally placed immediately after the main function, although they appear in any order.
All sections, except the main function section may be absent when they are not required.


Post a Comment