Structure of a C Program

To find out the structure of a C program, consider following

Example

#include<stdio.h>
int main()
{
    int sum;
    printf("Hello World, sum is : ");
    sum=sum1(2,2);
    printf("%d",sum);
    getchar();
    return 0;
}

int sum1(int x, int y)
{
    return x+y;
}

Any C program generally having structure as :-

  1. Include Header Files
  2. Define Global Variables
  3. Define main Function
  4. Execute Statements, Call Other Functions
  5. Define Other Functions

If we consider the above program, then above lines are explained as :

Line Number Explanation
1st We have include header files which are necessary for inclusion because of printf() and other standard input output functions.
2nd To define main() function, it’s the first function that compiler executes and also the starting of out program because this functions is executed at first. Other statements are executed later on.
3rd Opening Curly Brace for starting main() function block.
4th A variable (sum) of type Integer is defined.
5th printf() is a standard function that is used to output data to the console output(computer screen).
6th integer sum is assigned value of 2 variables by calling the function sum1 with parameters 2,2
7th to output sum to computer screen.
8th getchar() is a function which is used to stop computer screen after outputting a value, because if we don’t put it the screen disappear after output of text, so getchar() will wait to type a character from keyboard until output window will not be disappeared
9th A return code 0 is returned by function to the Operating system. So that the operating system knows that program executed successfully. (not necessary)
10th main function closed
12th sum1() function is defined with two temporary parameters.
14th sum of x and y is returned