A typical c program has the following structure:
- Pre Processor Directives
- Declaration Of Global Variables
- Function Prototype
- Main Function
- User Defined Function definition.
//pre processor directive #include<stdio.h>//declaration of global variables int k=0;//function prototype void add(int,int); //main function void main() { int a=10; add(a,k); } //User defined Function definition void add(int value1,int value2) { printf("sum:%d",value1+value2); }
Pre Processor Directives:
These are the directives to the compiler, they are followed while compiling the code.
ex:
1.#include<****.h>:directive to include the header file during
compilation
2.#define PI 3.1419 directive defining the value of constant PI,
wherever PI is used it will be replaced by 3.1419
Function Prototype:
A function prototype is nothing but signature of function.(for more info read article on functions)
ex:
int add(int,int);
It informs the compiler about the existence of a function add in the program.(whose body may be defined after the main function.)
Declaration Of Global Variables:
Global variables are those variables which can be used/ whose scope is throughout the program, ie these variables are common to main function and user defined functions.These variables have to be declared before the main function.
Main Function:
Every C program should have a main function, the execution of a program starts from main function.
However the we can call all other functions from a main function.(Read article on functions for more info on calling a function.).
User Defined Function Definition:
user defined function definition contains the logic for user defined functions.
ex:
void add(int var1,int var2)
{
printf("Sum:%d",var1+var2);
}
this function adds the two variables passed and prints the results on the computer screen.
similarly we can define custom functions to perform tasks desired by us.
No comments:
Post a Comment