A C program follows a standard structure that makes it organized, readable, and easy to maintain.
1. Documentation Section
- Contains comments describing the program.
- Explains the purpose, author, and date.
Example:
/* Program to print Hello, World! */
2. Link Section
- Includes the required header files using the
#includedirective.
Example:
#include <stdio.h>
3. Definition Section
- Defines symbolic constants using the
#definedirective.
Example:
#define PI 3.14159
4. Global Declaration Section
- Declares global variables and function prototypes.
- These can be accessed throughout the program.
Example:
int total;
void display();
5. Main() Function
- The execution of every C program starts from the
main()function. - It contains declarations, executable statements, and a return statement.
Example:
int main()
{
printf("Hello, World!\n");
return 0;
}
6. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- These are called from the
main()function.
Example:
void display()
{
printf("Welcome to C Programming");
}
Complete Example
/* Program to Demonstrate Structure of a C Program */
#include <stdio.h>
#define PI 3.14159
void display();
int main()
{
printf("This is the main function.\n");
display();
return 0;
}
void display()
{
printf("Welcome to C Programming.\n");
}
Output
This is the main function.
Welcome to C Programming.
Summary
| Section | Purpose |
|---|---|
| Documentation | Describes the program using comments |
| Link | Includes header files |
| Definition | Defines constants and macros |
| Global Declaration | Declares global variables and function prototypes |
main() Function | Starting point of program execution |
| User-Defined Functions | Performs specific tasks to improve modularity |
Category: MCA
