Structure of C Program

In this article, I’m gonna discuss the Structure of the C Program with Examples. Please read my previous published article, where we discussed the Library and IDE in C Programming Language.

Structure of C Program with Example

In order to understand the Structure of a C Program, Please have a look at the below image first.

Dheeraj Patidar

By Looking at the above image you will get rough idea of Structure of C Program. let’s understand each of the Section of C Program in Detail.

Documentation Section

Documentation section is all about commenting copyright stuff like program name, author name, date of creation, and many more.

This Section of C program structure can also be called comment section where Programmer can comment something which will  help other program to understand What the program is about?

This will help other programmers to understand your program code quickly. The Documentation section contains the following information.

  1. Project name
  2. Programmer name
  3. Creation date
  4. Programmer description, etc.

Preprocessor directives Section

In C Programming the statements which are starting with hash (#) character are called Preprocessor directives.

These statements are used to import in-build header files of C Programming. Like #include “stdio.h” consists of all standard input/output functions like printf(), scanf(), etc.

Link Section

The link section provides instructions to the compiler to link functions from the system library.

Definition section

The definition section defines all symbolic constants.

Global Declaration Section

The variables which are used in more than one function. Such variables are called global variables and are declared in the global declaration section whci reside out of all the functions.

In this section we can also declares all the user-defined functions. The Statements present in Global Declaration Section will be accessible by all the functions in the program.

Main() Function Section

Before discussing main() function we have to understand  What is function?

What is function?

Function is a block of code used to perform specific task. I t is not possible to write C Program without function. Every function in a C program must start with Open curly brace “{” and ends with Closed curly brace “}”.

Now let’s understand main() function in C Program. Every C program must have one main function section.

Main() function in C Program

Main function is a predefined function in c programming. This function is compulsory to to start writing any program because C program execution is starting from main function.

Without main function execution of program is not possible. 

We can also create other functions like main function but all other functions are calling directly or indirectly from main function. 

But main function is not calling from inside to program because it is a predefined function which is calling by operating system. 

We can pass arguments to main function by command line.

Subprogram Section

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.

Note: If you are not understanding all above concepts then don’t worry you will be clear about all the concepts with upcoming posts.

Example of C Program

 

/* Program to add two Numbers (Comment) */

#include<stdio.h> /*Header File with Preprocessor*/

void main() /*Main Function*/

{ /* Function Body Starting */

int a,b,c; /* Variable Declaration */

printf(\n Enter two Numbers”);  /* Output Statement for Taking Input */

scanf(%d%d,&a,&b); /* Input Statement */

c=a+b; /* Arithmetic Operation / Processing */

printf(\nSum= %d,c); /* Output Statement for Output Printing */

} /* Function Body End */

Explanation of Above Program Structure:

Comments  

Comments are used to understand program in better way.

Comments are not executed by Compiler. Comment are not part of program.

They are used for just small note about program Writer in program to understand

program clearly. We can add comment anywhere in c program after terminating Instruction using semicolon.

There are 2 types of comment available in C:

  • Single line comment: //This is a comment
  • Multi line comment: /*This is multi line comment*/

Header files

In C programming collection of header files is called library and collection of related predefined functions is called Header files. 

To use any predefined function in c program we need to to declare related header file of this function before main function.

C library is consist of n number of header files and these header files having number of related predefined functions. Each of the function in different different header file having special functionality to perform particular task.

There are n numbers of Preprocessors available in C and one of them is #include Preprocessor. With the help of Preprocessor #include we can Declare any header file in c program.

Ex.=> stdio.h header file contains n number of functions like printf(), scanf(), fprintf(), fscanf(), fopen(), fgets(), fputs(), etc…

Some Header files and their Functions :-

math.h => pow(), rand(), sqrt(), etc..

graphics.h => setcolor(), setbkcolor(); circle(), bar(), line(), outtextxy(), textstyle(), etc…

string.h => strlen(), strrev((, strcat(), strcpy(), strcmp(), etc…

conio.h => clrscr(), getch(), getche(), textcolor(), textbackground(), etc…

Main function

Main function is a predefined function in c programming. This function is compulsory to to start writing any program because C program execution is starting from main function. Without main function execution of program is not possible.

We can also create other functions like main function but all other functions are calling directly or indirectly from main function.

But main function is not calling Fromm inside to program because it is a predefined function which is calling by operating system. We can pass arguments to main function by command line.

Variable Declaration

we all know that to store any value in computer we need to help variables. Through variables we can easily Store values in computer and also accessing them easily. 

In the declaration of a variable it is mandatory to specify its data type. 

In C programming some predefined primitive data types are available to declare variables. 

These are int, float, char, and void.

  • int variable can hold one integer value in it
  • float variable can hold one real (floating) value in it.
  • char variable can hold one character value in it.
  • void variable can hold one value of any type.

Printf function

printf function is a predefined function of stdio.h header file. It is used in C program in 2 ways:-

  1. To take value from End-user.
  2. To print output of program.

Prototype or Syntax of printf function in 2 ways:-

  1. For Requesting input from End-user: printf(“request statement (char*string)”);
  2. For Printing output of program:-printf(“ouput statement (char*string)”, variables list);

scanf function

scanf is also predefined function of stdio.h header file which is used to store value given by End-user through printf function. 

To store value in variables we need address of operator (&). We can store n number of values in n Number of variables through scanf using format specifies and using address of operator (&) before variable name.

Prototype or Syntax of scanf function:-

scanf (“formate specifiers”,&variable1, &variable2,…);

Ex=> scanf(“%d%f%c”,a,b,c);

Rules application to a C program

There are some basic rules which are applicable to all the c programs

  1. Every program’s execution starts from the main function.
  2. All the statements are terminated with a semi-colon.
  3. Instructions are case sensitive.
  4. Instructions are executed in the same order in which they are written.
  5. For printing output of any C Program we Need to use predefined function of stdio.h header file Called printf( ).
  6. For taking input there is one more predefined function of stdio.h header file is used called scanf().

Flow of C Program in computer

Step 1: C program (source code) is sent to preprocessor first. The preprocessor generates an expanded source code.

Step 2: Expanded source code is sent to compiler which compiles the code and converts it into assembly code.

Step 3: The assembly code is sent to assembler which assembles the code and

converts it into object code. (.o or .obj).

Step 4: The object code is sent to linker which links it to the library such as

header files. Then it is converted into executable code. (.exe)

Step 5: The executable code is sent to loader which loads it into memory and

then it is executed. After execution, output is sent to console.

Conclusion

This is all about Structure of C Program. If you haven’t understand all aspects then don’t worry I will share more posts in C Programming Series.

I hope you have got useful & valuable content from this article. Keep reading this blog.

Thanks for Visit!


< Previous Post

  Next Post >


Leave a Reply

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