Tutorials

C Basics


Basic structure C program


Basic structure of a C program is defined by set of rules called protocol. 

As a good programming practice these rules should be followed by programmer while writing C program.

 

All C programs have following sections/parts

  1. Documentation section
  2. Link Section
  3. Definition Section
  4. Global declaration section
  5. Function prototype declaration section
  6. Main function
  7. User defined function definition section

 

DESCRIPTION FOR EACH SECTION OF THE C PROGRAM:

Documentation section

 We can give comments about the program, creation or modified date, author name etc in this section.

The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.

For example : /* comment line1 comment line2 comment 3 */

 

Link Section   

Link section contains Header files that are required to execute a C program are included in this section

 

Definition Section       

In this section, variables are defined and values are set to these variables.

 

Global declaration section       

Global variables are defined in this section.

When a variable is to be used throughout the program, it can be defined in this section.

 

Function prototype declaration section               

Function prototype gives many information about a function like return type, parameter names used inside the function.

 

Main function

Every C program is started from main function and this function contains two major sections called declaration section and executable section.

 

User defined function section               

User can define their own functions in this section which perform particular task as per the user requirement.

 

Now we will see an example (program) that would also demonstrate each section in details.

 

C program example

/*
    Documentation section
    C programming basics & structure of C programs
    Author: Nikhil pandey
*/
 
#include <stdio.h>   /* Link section */
#include<conio.h>    /* Link section */
int total = 0;       /* Global declaration, definition section */
int sum (int, int);  /* Function prototype*/

void main()          /* Main function */
{
    printf ("This is a C basic program \n");
    total = sum (1, 1);
    printf ("Sum of two numbers : %d \n", total);
    Getch();
}
 
int sum (int a, int b) /* User defined function */
{   
    return a + b;      /* definition section */
}

In the above example we have already seen almost all the sections a C program could have. 

 

Though its not necessary to have all of them in the same program, if not required. As in the last chapter of this C tutorial, you saw the simplest code in which many of the above defined section was not included as they were not needed.

 

 

 



Please Share this page
Views : 58
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!