Basic Structure of C Program
C Program consist of following parts: |
||||||
| (1) Comment | ||||||
Comments are
used in the program for documentation purpose. Using comment programmer can specify
Logic behind the code so other programmer or user can easily understand the program. Example
/* This Program finds weather given number is odd or even */ |
||||||
| (2) Header File | ||||||
C has lots
of built in functions organized into various Library files (Header Files). In Order
to use particular built in function we need to include particular Header file in
the Program. Example
#include<stdio.h> stdio.h header file contains definition of various standard input output functions. |
||||||
|
|
||||||
| (3) Macro Substitution (Defining Constant) | ||||||
Macro Substitution
is used in C program to define Symbolic Constant. Symbolic Constant can be defined
using #define preprocessor directive. Example
#define PI 3.14 It will replace all occurance of PI in the program with the value 3.14 before compiling the Program. |
||||||
| (4) Global Variable Declaration | ||||||
Global variables
are those variables that can be used by all the functions in the program. Global
variables are declared outside all functions. |
||||||
| (5) Main Function | ||||||
main() is
a starting point of C Program. C Program starts its execution from main(). void main()
{ } void indicates main() function does not returns any value to operating system. |
||||||
| (6) Function Definations | ||||||
User Define
Functions declared inside main() function are defined after main () function. int sum(int a, int b) { int c; c= a+b; return c; } |
||||||
Sample C Program |
||||||
/* Simple Program to find area of circle */
#include<stdio.h> #include<conio.h> #define PI 3.14 void main() { float a,r; float area(float r);/* Function Declaration*/ clrscr(); printf("Enter Radius:"); scanf("%f",&r); a=area(r); /* Calling Function */ printf("Area of Circle is %f",a); getch (); } float area(float r) { float a; a=PI*r*r; return a; } |