A C program is organized into declarations, definitions, statements, and functions. The main() function serves as the entry point of a hosted C program.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(10, 20);
printf("%d\n", result);
return 0;
}
Example Output #
30
Explanation #
The program contains several structural elements.
#include <stdio.h>
The preprocessor directive includes the standard I/O header so that printf() can be used.
int add(int a, int b)
{
return a + b;
}
This defines a function named add. It accepts two int parameters and returns an int.
int main()
defines the program’s main function.
{
int result = add(10, 20);
printf("%d\n", result);
return 0;
}
The braces define the body of main. The statements inside the body execute when main is called as the program’s entry point.
Basic C Program Elements #
| Element | Example | Purpose |
|---|---|---|
| Preprocessor directive | #include <stdio.h> |
Processed before compilation |
| Function definition | int add(...) |
Defines a function |
| Declaration | int result; |
Declares an object |
| Initialization | int result = 30; |
Gives an initial value |
| Expression statement | result = 10; |
Evaluates an expression |
| Function call | printf(...) |
Calls a function |
| Compound statement | { ... } |
Groups declarations and statements |
| Return statement | return 0; |
Returns from a function |
A C source file can contain multiple functions, declarations, definitions, and preprocessor directives. The exact organization depends on the program, but execution in a hosted environment begins with main().