Index
In C programming, every program must follow a specific structure to be understood and executed by the compiler. Here’s an in-depth look at each part of a simple C program structure.
Preprocessor Directives
- A C program begins with preprocessor directives, which are instructions for the compiler that start with the
#symbol. - These directives are handled by the preprocessor, a part of the compilation process that runs before the actual compilation of code.
- Common directives include
#include(for including libraries) and#define(for defining constants). - For example,
#include <stdio.h>includes the standard input-output library, which provides functions likeprintfandscanffor displaying output and taking input, respectively.
#include <stdio.h> // Preprocessor directive to include standard I/O library
The main() Function
- In C,
main()is the starting point of every program, and its declaration marks the entry of the program. This is where the operating system (or the runtime environment) begins executing your code. - The
main()function has a return type ofint, which means it returns an integer value at the end of its execution. Returning an integer frommain()provides a status code back to the operating system.- By convention, returning
0indicates the program completed successfully, while other values might indicate errors or specific status codes.
- By convention, returning
- The
main()function is often written asint main()orint main(void), wherevoidspecifies that it takes no parameters. - Within the
main()function, code statements are enclosed within curly braces{ }.
int main() {
// Code execution starts here
}Statements and Expressions
- Statements are the individual instructions within the
main()function that direct the program’s operations, typically ending with a semicolon (;). - Expressions evaluate to a value and can be as simple as a mathematical calculation or a call to a function.
- The
printffunction is a common statement used for displaying output on the screen, as shown inprintf("Hello, World!\n");. - Comments (like
// Print statement) can be added to describe code but are ignored by the compiler. In the example,// Print statementis a single-line comment that explains the purpose of theprintfline.
printf("Hello, World!\n"); // Output: Hello, World!Return Statement
- Since
main()is defined with a return type ofint, it should end with areturnstatement. This is a way for the program to signal to the operating system whether it has finished successfully or encountered an error. return 0;tells the system that the program executed successfully. Different return values can be used to indicate different statuses or error codes, though0is the standard success indicator.
return 0;Example Program in Action
Here’s how the structure looks in a complete program:
#include <stdio.h> // Includes standard input-output library
int main() { // main function - entry point
printf("Hello, World!\n"); // Output statement
return 0; // End of program with success status
}
- Step-by-Step Explanation:
- The program starts by including the standard I/O library.
- The
main()function is defined as the program’s entry point. - Within
main(),printfis called to print “Hello, World!” on the screen. - Finally,
return 0;ends the program, signaling that it completed successfully.

