Explain the Basic Structure of a C Program with an Example.



Basic structure of C programming: 

To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are illustrated below.

Write down the basic structure of C programming.
  1. Documentation section: The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later.
  2. Link section: The link section provides instructions to the compiler to link functions from the system library such as using the #include directive.
  3. Definition section: The definition section defines all symbolic constants such using the #define directive.
  4. Global declaration section: There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions.
  5. main () function section: Every C program must have one main function section. This section contains two parts; declaration part and executable part
    1. Declaration part: The declaration part declares all the variables used in the executable part.
    2. 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 is the logical end of the program. All statements in the declaration and executable part end with a semicolon.
  6. Subprogram section: If the program is a multi-function program then the subprogram section contains all the user-defined functions that are called in the main () function. User-defined functions are generally placed immediately after the main () function, although they may appear in any order.

All section, except the main () function section may be absent when they are not required.

ContentMiddleAd


Following is a sample C Program that contains all of the above mentioned sections


/*
 * Name: Sample Program
 * Author: OnlineClassNotes.COM
 * Create Date: 26/11/2016
 * Last Modified: 27/11/2016
 */

//Linking required library
#include <stdio.h>

//defining a constant
#define MAX_ARRAY_LENGTH 20

//defining a global variable
int max_input_length = 15;

//declaring an user defined function which has been defined later
float addNumbers(float a, float b);

int main(){
    //declaring a local variable for main()
    int localVariable = 50;
    
    //Accessing a defined constant
    printf("Max array length is %d\n", MAX_ARRAY_LENGTH);
    
    //Accessing a global variable
    printf("Max input length is %d\n", max_input_length);

    //Accessing an user defined function, declared in gloabl declaration section
    printf("Summation of 5.5 and 8.3 = %f\n",addNumbers(5.5,8.3));
    
    return 0;
    }

//defining an user defined function which has been declared earlier.
float addNumbers(float a, float b){
    return (a+b);
    }

In this above sample C Program
  • Line 1-6: These are comments, these lines will not be executed. This is the Documentation Section.
  • Line 9: This is the Link Section. Here we are linking our program to a header file or library called stdio.h, this library provides methods for sending output to screen or taking input from user using standard output and input devices.
  • Line 12: This is the Definition Section. Here we are defining a constant.
  • Line 15 & 18: This is Global Declaration Section. In line 15 we are declaring a global variable and in line 18 we are declaring an user defined function
  • Line 20-34: This is the Main Function Section. Every C Program must have this section. This section can have to sub-sections. Declaration Part and Executable Part. However, this two part can be mixed. In line 22 we are declaring local variable. Line 25, 28, 31 and 33 are executable statements.
  • Line 37-39: This is the Sub-Program Section. Here we have defined an user defined function that we have previously declared in the Global Declaration Section.

Recommended Recommends

Comments

Contact Us