Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the different sections in C language
A C program follows a structured format with different sections that serve specific purposes. Understanding these sections helps in writing well-organized and maintainable code.
Structure of a C Program
The complete C program is divided into different sections, which are as follows −
Documentation Section − Contains comments about the program like author name, creation or modification date. Information written between
/* */or after//is called a comment line. These lines are ignored by the compiler during compilation.Preprocessor Directives Section − In this section, header files required to execute the program are included using
#includedirective.Global Declaration Section − Here, global variables and symbolic constants are defined which can be used throughout the program.
Function Prototype Declaration Section − This section declares function prototypes that specify return type, function name, and parameters without the actual function body.
Main Function − The C program execution starts from this section. It contains local declarations and executable statements.
User Defined Function Section − Users can define their own functions to perform specific tasks as per requirements.
Syntax
/* Documentation section */
#include <header_files>
/* Global declarations */
datatype global_variables;
/* Function prototypes */
return_type function_name(parameters);
int main() {
/* Local declarations */
datatype local_variables;
/* Executable statements */
statements;
return 0;
}
/* User defined functions */
return_type function_name(parameters) {
/* Local declarations */
/* Executable statements */
return value;
}
Example: Complete C Program Structure
Following is a C program demonstrating all sections with a function to perform addition −
/* Documentation Section
Program: Addition of two numbers
Author: Tutorial Example
Date: 2024 */
#include <stdio.h>
/* Global Declaration Section */
int result;
/* Function Prototype Declaration */
void sum(int, int);
/* Main Function */
int main() {
/* Local Declaration */
int a, b;
/* Reading User Input */
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
/* Function Calling */
sum(a, b);
return 0;
}
/* User Defined Function Section */
void sum(int x, int y) {
/* Local Declaration */
int add;
/* Addition Operation */
add = x + y;
/* Printing Output */
printf("Addition of %d and %d is %d<br>", x, y, add);
}
Enter two numbers: 5 6 Addition of 5 and 6 is 11
Key Points
- The main() function is mandatory and serves as the entry point of the program.
- Documentation helps in code maintenance and understanding.
- Function prototypes must be declared before use or defined before the main function.
- Global variables are accessible throughout the program, while local variables are limited to their scope.
Conclusion
Understanding the structure of a C program with its different sections is fundamental for writing organized code. Each section has a specific purpose that contributes to the overall functionality and readability of the program.
