BEST TUTORIAL FOR PROGRAMMING        
BEST TUTORIAL FOR PROGRAMMING

Structure of C Program

Here, we will learn about structure of a C program in detail. To know about basic structure of a C program is very essential and important for you before proceeding to learn more about C programming. Knowledge of C program structure makes us capable to understand any C program easily and in proper way.




Structure of a C program

contains the following sections :
  1. Documentations (Documentation Section)

  2. Preprocessor Statements (Link Section)

  3. Global Declarations (Global Definition Section)

  4. The main() function

    1. Local Declarations (Variables)
    2. Program Statements & Expressions
  5. User Defined Functions (Sub-Programs Section)




Basic structure of C program

by sample :
/*
* Author : [ Author's Name ]
* Date : [ Date ]
* Description : [Program's Description ]
*/


[ Preprocessor Statements ]

[ Global Declarations ]

return_type main() {

[ Local Declarations (Variables) ]

[ Program Statements & Expressions ]

return [ Return Value ];
}

[ User Define Functions ]



Basic structure of a C program

by C code :
/*
* Author : Vktutorial
* Date : 2018-04-14
* Description : Prints "Hello World !!" on the screen.
*/


#include <stdio.h>
#include <conio.h>

int main() {
/* This statement prints "Hello World !!" on the console. */
printf("Hello World !!");

getch();
return 0;
}

Output :

Hello World !!

NOTE : Some sections of the C program structure can be absence. It is not necessary to include all the sections in all C programs. As we did not include Global Definition Section and Sub-Programs Section in the above C program.



Various parts of the above C program :

1  /* Comments */ : In programming, a comment is a programmer-readable explanation in the source code of a program. They are added in program source code with the purpose of making the program source code easier for humans to understand. Comments are ignored and not executed by compilers.

As you can see in the above, we used multi lines comment to provide information about the program like who wrote, when wrote, and what is purpose of the program.

Comment can be used anywhere in the program


2  #include<stdio.h> and #include<stdio.h> : #include is a preprocessor command that informs the compiler to include the header or library file in the program before compiling the source code. As we included stdio.h and conio.h header files in the above program using the #include preprocessor command.

stdio.h is a header or library file for "standard input / output". It contains many functions or commands for standard input / output. One of them (printf() function), we used in the above program to print "Hello World !!" on the screen.

conio.h is a header or library file for "console input / output". It contains many functions or commands for console input / output. One of them (getch() function), we used in the above program to hold screen to see output.


3  int before the main() function : int is a data type for integer value (1412) in C programming language. We used int before the main() function to define return value type. Return data type before the main() function tells that what type of value the main() function will return on successful execution or unsuccessful execution.

We will learn about return in detail in coming sections.


4  main() : The main() function is the main function where program execution begins. The main() function is the default entry point of every C program. Every C program must contain only one main() function.


5  {...} : Inside opening curly bracket and closing curly bracket, we can write C programming statements and expressions for the main() function. Opening curly bracket and closing curly bracket shows how much scope the main() function has.

Every statement or expression in C programming must be ended with semicolon (;).


6  printf() : It is a function of stdio.h library that used to print text on the screen. As we printed "Hello World !!" on the screen in the above program using this function.


7  getch() : It is a function of conio.h library that used to read a character from the console or terminal without echoing (display). Mostly it is used at the end of C programs to hold the screen. It makes sure that you can see your output on the output screen.

Note : If we don't use getch() function at the end of our C program, then we won't be able to see the output of our program.


8  return : return is a keyword of C language that is used to inside the main() function to return execution status to the operating system. If return value is 0 then it means that the program execution is successful otherwise it is assumed that there is a problem in the program.




Let's understand basic structure of C Program :

1  Documentations (Documentation Section) : The documentation section usually contains name of the program, author's or programmer's name, date, short description of the program and few other details.


2  Preprocessor Statements (Link Section) : The link section instructs the compiler to link the various functions from the library's functions.


3  Global Definition Section : The global definition section defines all the symbolic-constants.


4  Global Declaration Section : The global declaration section is used to define those variables and functions (user-defined functions) that are accessed and used globally within the entire program.


5  main() function : The main() function is the main function where program execution begins. The main() function is the default entry point of every C program. Every C program must have main() function and only one.

The main() function also contains two sections.

Declaration Section :- It is used to declare all local variables that will be used within the main() function.

Execution Section :- It contains statements and expressions.


6  User Defined Functions (Sub-Programs Section) : The sub-programs section contains all user-defined functions.




Next Topic : C Program Flowchart...