Structure of C Program with Example

Before start writing C program we should know the basic structure of C program and how the whole program works together so that we can easily debug if an error occurs during program execution.

Look at the below example structure:

//Example to explain basic structure of C Program

#include <stdio.h>  //Linking section & "stdio.h" is a header file

#define Num 10  //defining constant
int GV=13;  //global declaration

int multi(int,int);  //global declaration

int main()  //starting point of C program
{
int a=1, b=2;      //varaible declaration
int ab;

printf("Printing a, b, and Num value %d  %d  %d \n\n",a,b,Num);
                              //executable part

printf("Global variable accessible here GV = %d \n\n",GV);

ab = multi(a,b);       //calling function or subprogram

printf("Multiple of a X b = %d \n\n",ab);

return 0;

}


//SUB-Program

int multi(int a, int b)
{

printf("Global variable accessible in all functions GV = %d \n\n",GV);
                         //Global variable accessible here too

return a*b;

}

//OUTPUT

Printing a, b, and Num value 1  2  10

Global variable accessible here GV = 13

Global variable accessible in all functions GV = 13

Multiple of a X b = 2

Documentation Section

As the term defines it contains all information such as comment to increase readability of code to other programmers or people, author details, name of the program etc. which are ignored by compiler during compilation of code.

Comments written in two different ways in C language:

  1. Single Line Comment
  2. Multi Line Comment
// This is single line comment ignored by compiler


/*This is multi line
Comment.
You can write any
number of lines here.*/

Linking Section

The linking section provides instructions to the compiler to link header files and functions from system library. Header files (.h) contains function prototype of Standard Library included at the beginning using preprocessing directive #include as given below the program in C.

#include <stdio.h>

All the files, header or constant following # replace by their respective code in compiled version of a program.

Definition Section

This section defines all symbolic constants such using the #define directive. A symbolic constant value cannot be changed in program after it is declared and defined.

#define Num 10

Global declaration

Global section contains variables that are available to more than one function termed as called global variables. These variable are declared outside of all the functions and before main(). This section also contain prototype of all user-defined functions.

int GV=13;

main () function

Every C program must have only one main function which indicate the starting point of a program to compiler. This section contains declaration and executable parts.

Declaration: Declaration part contains all the variables that are declared & defined required in executable part.

Executable part: There must be at least one executable statement in the main section.

These two parts declaration and executable must be written between the opening and closing braces. The execution begins at the opening bracket ({) and ends at the closing bracket (}). All statements in C must end with a semicolon.

int main()
{
...   //statements to execute
...
}

Semicolon

In a C semicolon indicates end of the statements. Every statement must end with a semicolon.

printf("Hello, World! \n");

Subprogram

These section contains all the user-defined functions that are called in the main () function or by any function. Generally, a user-defined functions placed after the main () function, but can follow any order.

int multi(int a, int b)

Don’t forget to share and subscribe

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: