« Previous                                                                  Next Chapter »
Structure of C Program

In order to develop a complete C program, We have to fallow below sections in the program.

Documentation section           //Optional
Link section                              //Optional
Definition section               
     //Optional
Global declaration section     //Optional
Main( ) function section        //Must
{
Declaration part; 
Executable part (statements);
 
}
Subprogram section              
//Optional
Function 1
Function 2
       -
       -
Function n



Documentation Section

This is the first section in the C program. In this section user can provide the complete information about the program which includes Name of the author, Name of the program, Aim of the program, Logic of the program, The number of variables used in the program, The number of functions and its dependent on which function etc.

All the information in this section is to be placed in /*  and */. So the compiler doesn’t try to compile this section. This section is purely for providing help to end user programmer.


Link Section

This is the second section in the C program. This section is useful to attach some header files to the program. A header file is a file which contains all the pre developed functions by the developers of the programming language. When end user want to make use of any function, first he has to find in which header file the function is available. Later that header file must be attached to the program in this section. Then only user can make use of that function. Syntax to attach the header file as follows  
Syntax: # include < name of the header file name >

We have so many header files in C language. Some of them are stdio.h, stdlib.h, math.h, conio.h, dos.h, dir.h
For example to include stdio.h:
 # include < stdio.h > 
Some functions in stdtio.h are 1)printf(); 2)scanf(); 3)puts() 4)gets(); 5)fputs()


Definition Section

This section is used to define or set values to variables.


Global Declaration Section

There are some variables that are used in one or more functions, such variables are called global variables and are declared in this section.


Main() function Section

Every C program must have one main () function section. This section can contain two parts Declaration part and Execution part.

  • These two parts can appear between the opening ({) and closing (}) braces. The program execution begins at the opening braces and ends at the closing braces. The closing brace of the function section is the logical end of the program.
  • The declaration part declares all the variables used in the executable part.
  • There is at least one statement in the executable part.
  • All statements in the declaration and executable parts end with a semicolon (;).


Subprogram Section

The sub program 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.

Simple C program:
         /* Program to display message */

#include<stdio.h>

main()

{

printf(" Welcome to TutorialsGuru.Net ");

}


More Examples   
         /* Program to read and print message */

#include<stdio.h>

#include<conio.h>

main()

{

char x;

clrscr();

printf("Enter the character ");

x=getchar();

printf("The given character is ");

putchar(x);

}


/* Program to read and print the values */

#include<stdio.h>

#include<conio.h>

main()

{

int a;

char b;

float c;

clrscr();

printf(" Enter the int,char,float values ");

scanf(" %d %c %f",&a,&b,&c);

printf(" The given values are ");

printf(" %d %c %f",a,b,c);

}



  « Previous                                                                  Next Chapter »