Structure of C Programs
• A c program may consist of one or more sections as shown in figure.
• The documentation section consists of set of comment lines giving the
name of the program, the author details.
• The link section provides instruction to the compiler to link functions from
the system library.
• The definition section defines all symbolic constants.
• There are some variables used in more than one function. Such variables are
called global variables and are declared in global declaration section.
• Every C program must have one main() function.
• This section contains two parts, declaration and executable part.
• The declaration part declares all the variables used in executable part.
• There is at least one statement in the executable part.
• These two parts must appear between the opening and 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 must ends with a
semicolon.
• The subprogram section contains all the user defined function that are called
in main function.
• User defined function are generally placed immediately after the main
function.
#include<stdio.h>
This part of the code is used to declare all the
header files that will be used in the program.
This leads to the compiler being told to link
the header files to the system libraries.
float area(float r);
int a=7;
This part of the code is the part where the
global variables are declared. All the global
variable used are declared in this part. The
user-defined functions are also declared in this
part of the code.
Sample C program
EXAMPLE
/* File Name: areaofcircle.c
* Author: Manthan Naik
* description: a program to calculate area of circle
*/
#include<stdio.h>//link section
#define pi 3.14;//definition section
float area(float r);//global declaration
int main()//main function
float r;
printf(" Enter the radius:n");
scanf("%f",&r);
printf("the area is: %f",area(r));
}
float area(float r)
return pi * r * r;//sub program