Tuesday 18 December 2018

Structure of a “ C “ program


Documentation Section :
 This section consists of a comment tags giving the name of the program , the author and other details of the program which the programmer would like to use later. 

The comments are enclosed using
 /* comment */ 
         (or) 
//comment.
Ex : /* program for addition of 2 numbers */

Preprocessor Directives or Link Section :
It provides instructions to the compiler to link the functions for executing a program. 
It is also used to define symbolic constants of the program. 
To write a program this section is used to include predefined or user-define header files. 
A header file has an extension .h and these are included using “ #include “. 
Ex : #include (standard input/output header file)
       #inclide (console input/output header file)
       #define PI 3.14 (assigns symbolic name PI to the constant value 3.14)

Global Declaration Section : 
The variables that declare outside of the main function are called Global variables. 
These variables can be accessed anywhere by any function throughout the program. 
The global variables are automatically initialized with zero hence there is no chance of garbage value. The global variable declaration can be done as datatype variable1,variable2,……; 
Ex : int a,b;

Main() function Section :
Every C program must have only one main() function. 
Without this section the program will not run. 
This is the starting point for program execution.
 This section contains two parts 
1. Declaration Part 
2. Execution Part

Declaration part : 
Variables which are used in the main() program are declared by using the variable declaration statement called as Local Variable Section. 
Local variables can be declared as :
 datatype variable1,variable2,……….;
 Ex : int a,b,c; float x,y; char ch;

Execution statements : 
This section has reading, writing and processing statements having input/output functions, conditional statements, looping statements, function calling statements etc. 
this section executes all the statements and there must be atleast one statement in the execution section. 
Ex : printf(“hai welcome”); 
c=a+b; 
Both Declaration and Execution section must appear in between the opening
 “ { “ brace and closing “ } “ brace.
 The closing brace of the main() function is the logical end of the program. 
All the statements in the declaration and executable section must end with a semicolon “ ; “. 

Sub program Section or User-define function Section : 
This section contains all user-defined functions that are called in main function. 
Every sub program function section has local variable declaration section and executable section. Instead of writing the group of statements again and again in the main() function program, we can write it once in the function sub program and call the function again and again in the main() program according to the user requirement.

No comments:

Post a Comment