This article demonstrates the Structure of a C Program. Basically, C is a high-level programming language. Unlike C#, Java, and C++, it is a structured programming language. While, all these languages are object-oriented programming languages.
A C program may comprise of a single file or more than one file. It contains number of variable declarations and function definitions. In general, the Structure of a C Program has following elements:
- Documentation or comments
- Preprocessor directives
- Global Variable Declaration
- Function declarations
- main function
- user-defined functions
While, the comments represent statements that the compiler doesn’t execute. So, comments are ignored by the compiler during execution. Further, the comments may be a single line comment (//) or a multiple-line comment (/*….*/).
The preprocessor directives allow the compiler to run a certain program before compilation starts. Evidently, we use preprocessor directives to include header files, to define constants and so on. For example, some of the preprocessor directives are #include, #define, #ifdef, #if, #pragma, #error, and so on.
Similarly, we declare global variables in the global variable declarations. Basically, we can use the variables declared in the global section in any function of the program.
The execution of a C program starts with the first executable statement in the main function. In fact, we call other user-defined functions from the main function.
While, a C program may contain zero or more user-defined functions. A function performs a well-defined task. Function declaration section contains the first line of the function followed by a semicolon. In general, the first line of the function comprises of the return type, function name, and parameter list within parenthesis.
Finally, the user-defined functions section contains the function definitions.
An Example Demonstrating the Structure of a C Program
The following code shows an example of a simple C program. It shows how the global variables a and b are used in the main() function as well as in two user-defined functions.
/* A Sample C Program Demonstrating the Structure of a C program.
It is a multiline comment.
*/
//It is a single-line comment
//Preprocessor Directive for including header files
#include <stdio.h>
//Global declaration section
int a, b;
//Function Declaration section
int findsum(int x, int y);
int findproduct(int x, int y);
//main function
int main()
{
a=5;
b=6;
int sum=findsum(a, b);
int product=findproduct(a, b);
printf("sum = %d\nprodut = %d", sum, product);
return 0;
}
//user-defined functions
int findsum(int p, int q)
{ a=10; b=20;
return a+p+b+q;
}
int findproduct(int u, int v){
a=30; b=40;
return (a+u)*(v+b);
}
Output
Further Reading
50+ C Programming Interview Questions and Answers
C Program to Find Factorial of a Number
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML