Structure of a C Program
The structure of a C program consists of several key components that facilitate its functionality.
Preprocessor Directives: These lines, beginning with #, are processed before compilation. Common directives include #include
Function Declarations (Optional): Functions can be declared before their use, which is helpful for organizing code.
Main Function: The entry point of any C program is the main() function. Execution starts here, and it typically returns an integer.
c
int main() {
// Code
return 0;
}
Variable Declarations: Variables are declared to store data, such as int a; or float b;.
Statements and Expressions: This includes operations like assignments, calculations, and input/output commands (e.g., printf, scanf).
Control Structures: These structures, such as if, for, and while, control the flow of the program.
Functions (Optional): Additional functions can be defined to modularize the code.
A simple example might be:
#include// Preprocessor directive
// Function declaration (optional)
void greet();
int main() { // Main function
int num; // Variable declaration
printf("Enter a number: "); // Output statement
scanf("%d", #); // Input statement
if (num > 0) { // Conditional statement
printf("You entered a positive number.\n");
} else if (num < 0> printf("You entered a negative number.\n");
} else {
printf("You entered zero.\n");
}
greet(); // Call to another function
return 0; // Return statement
}
// Function definition
void greet() {
printf("Hello, welcome to the C programming world!\n");
}