Structure of A C Program
Structure of A C Program
STRUCTURE OF A C PROGRAM
In general, a C program is composed of the following sections:
Section 1: Pre-processor directives
Section 2: Global declarations
Section 3: Functions
Section 1 and 2 are optional, Section 3 is compulsory.
Documentation Section
Pre-processor directives
void main()
{
Structure of a Declaration part
C
Program Executable part
}
Comments:
Comments are used to convey a message and used to increase the readability of a
program.
They are not processed by the compiler.
There are two types of comments:
1. Single line comment
2. Multi line comment
Single line comment
A single line comment starts with two forward slashes (//) and is automatically
terminated with the end of the line.
E.g. //First C program
Multi-line comment
A multi-line comment starts with /* and terminates with */.A multi-line comment is
used when multiple lines of text are to be commented.
E.g. /* This program is used to find
Area of the Circle */
Section 1.Preprocessor Directive section
The preprocessor directive section is optional.
The pre-processor statement begins with # symbol and is also called the pre-
processor directive.
1
CP8151 – Unit 1
E.g. C Program
Line5 Printf(“Hello”);
Line6 }
Line1 is a comment; Line 2 is a preprocessor directive. Line3 is a header of the function main.
Line 4, 5, 6 form the body of main function.
Example Program:
/*Addition of two numbers*/ Documentation Section
#include<stdio.h> Pre-processor directives
#define A 10 Definition Section
int c; Global declarations
int sum(int,int);
main() Main() functions
{
int b;
printf(“Emter the value for B:”);
scanf(“%d”,&b); Execution Part
c=sum(b);
printf(“\n Answer=%d”,c);
getch();
}
int sum(int y)
{
c=A+Y; Sub Program
return(c);
}
PROGRAMMING RULES
1. All statements should be written in lower case letters. Upper case letters are only used
for symbolic constants.
2. Blank spaces cannot be used while declaring a variable, keyword, constant and
function.
3. The programmer can write the statement anywhere between the two braces following
the declaration part.
4. The user can also write one or more statements in one line by separating
semicolon (;).Ex:
a=b+c;
d=b*c;
or
a=b+c; d=b*c;
5. The opening and closing braces should be balanced. For example, if opening braces are
four, then closing braces should also be four.