C is a foundation programming language that influences many modern programming languages. To write an efficient program in C, all developers must know about the structure of a program. C structure is divided into six parts of the program. In this article, we will break down the basic components of a C program and explain their roles.
Section of the C Program
Six basic sections in the C program are responsible for properly executing a program.
The documentation section consists of the description of the program, the name of the program, and the creation date and time of the program. This section is specified at the start of the program in the form of comments.
// description, name of the program, programmer name, date, time etc.
or
/*
Description, program name, programmer name, date, time etc.
*/
Anything that programmers write in the comments in the C program will be treated as documentation of the program and will not interfere with the given source code.
Preprocessor Section
All the program header files will be declared in the preprocessor section of the program. The header files help us access others’ improved source code in our code. We can copy the multiple fields inserted into our program before the process of compilation.
Example
#include<stdio.h>
#include<math.h>
Definition
The preprocessor directive is starts from the “#” symbol in the source code. There are multiple steps 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. When the name is encountered by the compiler. It will replaced by the actual piece of source code that is defined the after the #define preprocessor.
The following program demonstrates the #define preprocessor .
Program
#include <stdio.h>
#define PI 3.14
#define SQUARE(x) ((x) * (x))
int main() {
float radius;
float area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * SQUARE(radius);
printf("Area of the circle: %.2fn", area);
return 0;
}
Output
Enter the radius of the circle: 3
Area of the circle: 28.26
Global Declaration
This section includes all the global variables, function declarations, and static variables. Those variables and functions declared in the scope can be used anywhere in the program. In simple words , Any part of the program can access the variables and functions.
int age(int current);
Main() Function
In C language, The main function of the source code. The C compiler starts execution from the main() function. It can use global variables, static variables, inbuilt function and user-defined function. This function return type can void any valid integer type.
The following program demonstrates the main() function:
Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s <num1> <operator> <num2>n", argv[0]);
return 1;
}
int num1 = atoi(argv[1]);
int num2 = atoi(argv[3]);
char operator = argv[2][0];
int result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero.n");
return 1;
}
break;
default:
printf("Error: Invalid operator. Use +, -, *, or /.n");
return 1;
}
printf("Result: %d %c %d = %dn", num1, operator, num2, result);
return 0;
}
Sub Programs
This section includes the user-defined function called in the main() function. This function are generally written after the main() function irrespective of their order. The user-defined function is called from the main() function, the control of the program shifts to the called function, and when it encounters a return statement, it returns to the main() function.
The following program demonstrates the sub program:
Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s <num1> <operator> <num2>n", argv[0]);
return 1;
}
int num1 = atoi(argv[1]);
int num2 = atoi(argv[3]);
char operator = argv[2][0];
int result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero.n");
return 1;
}
break;
default:
printf("Error: Invalid operator. Use +, -, *, or /.n");
return 1;
}
printf("Result: %d %c %d = %dn", num1, operator, num2, result);
return 0;
}
Output
Enter two integers: 39
40
Enter an operator (+, -, *, /): +
Result: 39 + 40 = 79
Structure of C Program with Example
The following program demonstrates the C program:
Program
// Definition
#define X 20
int sum(int y);
int main(void) {
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
int sum(int y) {
return y + X;
}
The structure of a C program is essential for creating organized, efficient, and maintainable source code. By Understanding its key components – such as preprocessor directives, the main() function, variable declarations, function definitions, and control structures . A well-structured program act not only enhances readability and debugging but also promotes source code reusability and collaboration among developers.
FAQs
What is the main() function ?
This the entry point of a C program where execution begins
What are preprocessor directives?
The commands like #include that are processed before compilation.
Can I skip return in main()?
Yes, in C99 and later , but it’s good practice to include it.
How do I format output in C?
Use the printf() function with format specifiers (eg %d for integers %f for floats).
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.