Structure of C Program.
#include //stdio.h is the header file
main() // main function is the first function which is executed by a C program.
All C statements are written within main function.
{
// All C statements.
}
Functions
Every C program consists of one or more modules called functions. One of the functions must be called main( ).
The program will always begin by executing the main function, which may access other functions.
Any other function definitions must be defined separately, either ahead of or after main.
A function name is always followed by a pair of parenthesis, namely, ( ). And all function statements are enclosed within a pair of braces { }.
Example of C program to calculate area of circle:
#include
#include
void main()
{
float a;
int r=2;
float pi=3.14;
a=pi*r*r;
printf("%d",a);
getch();
}
# is the preprocessor directive which commands that the content of file should be included at the time of compilation.
< stdio.h> and is the header file which contains all input and output functions like scanf(), printf() which are frequently used in c programs.
main() is the first function which is executed by a C program.
1. float a and int r=2. This statement declares 2 variables a and r of type integer and float. r has been assigned a value of 2.
2.float pi=3.14 -> this statement declare pi as float and value 3.14 has been assigned to it.
3. a = pi * r *r; -> This statement computes the area of circle and assign it to variable a.
4. printf("%f",a); -> This statement prints the area of circle using printf function.
5. getch(); -> It is used to get character from user.