Basic Structure of the C Program

 

BASIC STRUCTURE OF A C PROGRAM:

Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned  below.

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

EXAMPLE C PROGRAM TO COMPARE ALL THE SECTIONS:

You can compare all the sections of a C program with the below C program.

/*

    Documentation section

    C programming basics & structure of C programs

    Author: fresh2refresh.com

    Date : 01/01/2012

*/

 

#include <stdio.h>   /* Link section */

int total = 0;       /* Global declaration, definition section */

int sum (int, int);  /* Function declaration section */

int main ()          /* Main function */

{

    printf ("This is a C basic program \n");

    total = sum (1, 1);

    printf ("Sum of two numbers : %d \n", total);

    return 0;

}

 

int sum (int a, int b) /* User defined function */

{  

    return a + b;      /* definition section */

}                                                                                

 

This is a C basic program
Sum of two numbers : 2

 

DESCRIPTION FOR EACH SECTION OF THE C PROGRAM:

·         Let us see about each section of a C basic program in detail below.

·         Please note that a C program mayn’t have all below mentioned sections except main function and link sections.

·         Also, a C program structure mayn’t be in below mentioned order.

 

Sections

Description

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.
Example : /* comment line1 comment line2 comment 3 */

Link Section

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, 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.

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction