Structure of the C Program
Last Updated :
11 Sep, 2024
The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format. C program must follow the below-mentioned outline in order to successfully compile and execute. Debugging is easier in a well-structured C program.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned below:
- Documentation
- Preprocessor Section
- Definition
- Global Declaration
- Main() Function
- Sub Programs
1. Documentation
This section consists of the description of the program, the name of the program, and the creation date and time of the program. It is specified at the start of the program in the form of comments. Documentation can be represented as:
// description, name of the program, programmer name, date, time etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and this will not interfere with the given code. Basically, it gives an overview to the reader of the program.
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program. Header files help us to access other's improved code into our code. A copy of these multiple files is inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of compilation. There are multiple steps which are involved in the writing and execution of the program. Preprocessor directives start with the '#' symbol. The #define preprocessor is used to create a constant throughout the program. Whenever this name is encountered by the compiler, it is replaced by the actual piece of defined code.
Example:
#define long long ll
4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables. Variables and functions which are declared in this scope can be used anywhere in the program.
Example:
int num = 18;
5. Main() Function
Every C program must have a main function. The main() function of the program is written in this section. Operations like declaration and execution are performed inside the curly braces of the main program. The return type of the main() function can be int as well as void too. void() main tells the compiler that the program will not return any value. The int main() tells the compiler that the program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is shifted to the called function whenever they are called from the main or outside the main() function. These are specified as per the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}
Structure of C Program with example
Example: Below C program to find the sum of 2 numbers:
C
// Documentation
/**
* file: sum.c
* author: you
* description: program to find sum.
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
int sum(int y);
// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
// Subprogram
int sum(int y)
{
return y + X;
}
Explanation of the above Program
Below is the explanation of the above program. With a description explaining the program's meaning and use.
Sections | Description |
---|
/** * file: sum.c * author: you * description: program to find sum. */ | It is the comment section and is part of the description section of the code. |
---|
#include<stdio.h> | Header file which is used for standard input-output. This is the preprocessor section. |
---|
#define X 20 | This is the definition section. It allows the use of constant X in the code. |
---|
int sum(int y) | This is the Global declaration section includes the function declaration that can be used anywhere in the program. |
---|
int main() | main() is the first function that is executed in the C program. |
---|
{...} | These curly braces mark the beginning and end of the main function. |
---|
printf("Sum: %d", sum(y)); | printf() function is used to print the sum on the screen. |
---|
return 0; | We have used int as the return type so we have to return 0 which states that the given program is free from the error and it can be exited successfully. |
---|
int sum(int y) { return y + X; } | This is the subprogram section. It includes the user-defined functions that are called in the main() function. |
---|
Steps involved in the Compilation and execution of a C program:
- Program Creation
- Compilation of the program
- Execution of the program
- The output of the program
Read more about Compiling a C Program Compilation - Behind the Scenes
Conclusion
In this article points we learned about the structure of the C Program are mentioned below:
- The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format.
- Debugging is easier in a well-structured C program.
- There are 6 sections in a C Program that are Documentation, Preprocessor Section, Definition, Global Declaration, Main() Function, and Sub Programs.
- There are certain steps while compilation and executing of C program as mentioned below:
- Creation of Program
- Compilation of the program
- Execution of the program
- Output of the program
Similar Reads
Structure Pointer in C A structure pointer is a pointer variable that stores the address of a structure. It allows the programmer to manipulate the structure and its members directly by referencing their memory location rather than passing the structure itself. In this article let's take a look at structure pointer in C.L
3 min read
Output of C programs | Set 44 (Structure & Union) Prerequisite: Structure and UnionQUE.1 What is the output of this program? C #include <stdio.h> struct sample { int a = 0; char b = 'A'; float c = 10.5; }; int main() { struct sample s; printf("%d, %c, %f", s.a, s.b, s.c); return 0; } OPTION a) Error b) 0, A, 10.5 c) 0, A, 10.500000
2 min read
Output of C Program | Set 20 Predict the outputs of following C programs. Question 1 C int main() { int x = 10; static int y = x; if(x == y) printf("Equal"); else if(x > y) printf("Greater"); else printf("Less"); getchar(); return 0; } Output: Compiler Error In C, static variables can only be in
2 min read
Output of C Program | Set 18 Predict the output of following C programs.Question 1 C #include<stdio.h> int fun() { static int num = 40; return num--; } int main() { for(fun(); fun(); fun()) { printf("%d ", fun()); } getchar(); return 0; } Output: 38 35 32 29 26 23 20 17 14 11 8 5 2Since num is static in fun(), t
2 min read
Output of C Programs | Set 1 Predict the output of below programs. Question 1c#include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; }Output: Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2c #include<stdio.h
2 min read
Output of C Programs | Set 6 Predict the output of below programs Question 1 c int main() { unsigned int i=65000; while ( i++ != 0 ); printf("%d",i); return 0; } Output: 1 Explanation: It should be noticed that there's a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the c
4 min read
Output of C Program | Set 17 Predict the output of following C programs. Question 1 C #include<stdio.h> #define R 10 #define C 20 int main() { int (*p)[R][C]; printf("%d", sizeof(*p)); getchar(); return 0; } Output: 10*20*sizeof(int) which is "800" for compilers with integer size as 4 bytes. The pointer p is de-
2 min read
Output of C program | Set 25 Predict the output of following C program. C int main(void) { struct str { int i: 1; int j: 2; int k: 3; int l: 4; }; struct str s; s.i = 1; s.j = 2; s.k = 5; s.l = 10; printf(" i: %d \n j: %d \n k: %d \n l: %d \n", s.i, s.j, s.k, s.l); getchar(); return 0; } The above code is non-portable
2 min read
Output of C programs | Set 8 Predict the output of below C programs. Question 1: c #include<stdio.h> int main() { int x = 5, p = 10; printf("%*d", x, p); getchar(); return 0; } Output: 10 Explanation: Please see standard printf function definition int printf ( const char * format, ... ); format: String that cont
3 min read
Output of C Programs | Set 5 Predict the output of below programs Question 1 c int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output: Can't be predictedExplanation: The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and con
5 min read