๐๏ธ 2. Structure of a C Program
Do not memorize a C program line by line. Instead, learn to identify the role of every part of a program: comments โ preprocessor directives โ declarations โ main() โ statements โ function calls โ return.
Once you understand the structure, you should be able to look at an unfamiliar C program and explain what each part is doing.
A C program is a collection of declarations, definitions, statements, expressions, functions and other language elements arranged according to the rules of the C language.
There is no single mandatory six-section template that every C program must follow.
Textbooks often teach a traditional structure such as documentation, header files,
global declarations, main() and user-defined functions because it is useful
for beginners, but real C programs can be organized in different ways.
Instead of memorizing a fixed template, learn to recognize the purpose of each component.
For example, #include belongs to preprocessing, main() is the
entry function of a hosted C program, and statements inside a function describe actions
performed by that function.
A beginner-friendly C source file can look like this:
/* Documentation / comment */
#include <stdio.h>
#define MAX_VALUE 100
int globalValue = 10;
void displayMessage(void);
int main(void)
{
int number = 20;
printf("Number = %d\n", number);
displayMessage();
return 0;
}
void displayMessage(void)
{
printf("Hello from another function!\n");
}
C PROGRAM โ โโโ Comments โ โโโ Preprocessor directives โ โโโ #include โ โโโ #define โ โโโ Global declarations / definitions โ โโโ Function declarations โ โโโ main() โ โโโ local declarations โ โโโ statements โ โโโ return โ โโโ Other function definitions
Not every program contains every item shown above.
A small program may contain only #include, main(),
some statements and return 0;.
A large program may contain many header files, structures, functions,
macros, global objects and multiple source files.
Comments are notes written for programmers. They are ignored as comments during translation and do not become executable statements.
| Type | Syntax | Purpose |
|---|---|---|
| Single-line comment | // comment |
Useful for a short explanation. |
| Multi-line comment | /* comment */ |
Useful when an explanation spans multiple lines. |
#include <stdio.h>
int main(void)
{
// Display a message
printf("Hello, C!\n");
/*
The program ends
after printing the message.
*/
return 0;
}
Comments do not make a program execute differently simply because the comment contains programming instructions. For example:
// printf("Hello");
The commented-out printf() is not executed.
Preprocessor directives begin with #.
They are processed before the compiler translates the resulting C source.
#include <stdio.h>
#define PI 3.14159
int main(void)
{
printf("PI = %.2f\n", PI);
return 0;
}
A preprocessor directive is not a normal C statement.
For example, #include <stdio.h> does not need a semicolon.
A header file commonly provides declarations, macros, types and other information that source files need in order to use an interface.
#include <stdio.h>
int main(void)
{
printf("Hello\n");
return 0;
}
| Part | Meaning |
|---|---|
#include |
Preprocessor directive. |
<stdio.h> |
Standard input/output header. |
printf() |
Standard library function used for formatted output. |
The header provides declarations and related information to the source code. It does not mean that the header itself contains the executable implementation that is linked into your program.
In a hosted C environment, the program entry function is main.
A common standard form for a program that does not use command-line arguments is:
int main(void)
{
return 0;
}
| Part | Meaning |
|---|---|
int |
The function returns an integer value. |
main |
The designated program entry function. |
void |
Indicates that this form of main accepts no arguments. |
{ } |
Contains the function body. |
return 0; |
Returns a successful termination status from main. |
For a hosted C program, prefer standard forms such as
int main(void) or
int main(int argc, char *argv[]).
Do not teach void main() as standard hosted C.
The braces { and } define a compound statement.
For a function definition, they surround the function body.
int main(void)
{
printf("Hello\n");
return 0;
}
int main(void)
โ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโ
โ function body โ
โ โ
โ statements โ
โ declarations โ
โ expressions โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโ
The statements inside the braces belong to that function body.
A declaration tells the compiler about an identifier and its type or other properties. For example:
int age;
double salary;
char grade;
| Declaration | Meaning |
|---|---|
int age; |
Declares age as an integer object. |
double salary; |
Declares salary as a double object. |
char grade; |
Declares grade as a character object. |
The compiler needs information about identifiers so that it can understand how expressions use them and check whether those uses are valid according to C's rules.
A statement represents an action or control operation performed by the program. Examples include expression statements, selection statements, iteration statements and jump statements.
int x = 10;
x = x + 5;
printf("%d\n", x);
| Statement | Purpose |
|---|---|
int x = 10; |
Declaration with initialization. |
x = x + 5; |
Assignment expression statement. |
printf(...); |
Function-call expression statement. |
return 0; |
Jump statement that returns from the function. |
An expression is a combination of operands and operators that can be evaluated according to C's expression rules.
int a = 10;
int b = 20;
int sum = a + b;
Here:
aandbare operands.+is an operator.a + bis an expression.int sum = a + b;is a declaration with initialization.
Do not treat expression and statement as identical concepts. An expression can be part of a statement.
A function call requests that a function be executed with the supplied arguments.
printf("Hello\n");
The structure can be understood as:
Later, when you learn functions, you will see how to create your own functions
and call them from main().
The statement:
return 0;
returns the value 0 from main.
For a hosted environment, returning from main terminates the program
and provides a termination status to the host environment.
return 0; does not mean โprint zeroโ.
It returns a value from the function.
main() โ โ performs program work โ โ return 0; โ โ function ends โ โ successful termination status
Large programs should not put all logic inside main().
Programmers can divide functionality into separate functions.
#include <stdio.h>
void greet(void);
int main(void)
{
greet();
return 0;
}
void greet(void)
{
printf("Welcome to CodeBhavya!\n");
}
void greet(void);
greet();
void greet(void) { ... }
/* Program description */
#include <stdio.h>
#define MESSAGE "Hello, CodeBhavya!"
int total = 100;
void displayMessage(void);
int main(void)
{
int number = 10;
printf("%s\n", MESSAGE);
printf("Number = %d\n", number);
displayMessage();
return 0;
}
void displayMessage(void)
{
printf("Learning C step by step!\n");
}
| Part | Example | Role |
|---|---|---|
| Comment | /* Program description */ |
Documentation for programmers. |
| Header inclusion | #include <stdio.h> |
Makes standard I/O declarations available. |
| Macro definition | #define MESSAGE ... |
Defines a preprocessing macro. |
| Global definition | int total = 100; |
Defines an object at file scope. |
| Function declaration | void displayMessage(void); |
Declares a function before its use. |
| main() | int main(void) |
Program entry function in a hosted environment. |
| Local declaration | int number = 10; |
Defines a local object. |
| Function call | displayMessage(); |
Calls another function. |
| Function definition | void displayMessage(void) { ... } |
Provides the implementation of the function. |
This distinction becomes extremely important as you progress into functions, global variables and multiple source files.
| Concept | Example | Idea |
|---|---|---|
| Declaration | void greet(void); |
Tells the compiler about the function. |
| Definition | void greet(void) { ... } |
Provides the function implementation. |
| Object definition | int count = 10; |
Defines an object and gives it storage. |
A compiler may accept a function call when it has seen a proper declaration, but the final program still needs an appropriate definition when linking. This is one reason declarations and definitions must not be confused.
Consider this program:
#include <stdio.h>
void greet(void);
int main(void)
{
greet();
return 0;
}
void greet(void)
{
printf("Hello!\n");
}
The function definition appears after main(), but the compiler already knows
about greet() because of its declaration:
void greet(void);
The same function can be defined before main():
#include <stdio.h>
void greet(void)
{
printf("Hello!\n");
}
int main(void)
{
greet();
return 0;
}
Before a function is used in a way that requires a declaration, the compiler needs an appropriate declaration visible at that point. A function definition also serves as a declaration.
A useful beginner mental model is to separate the source structure from the execution flow.
Source code
โ
โ
Preprocessing
โ
โ
Compilation
โ
โ
Object / executable generation
โ
โ
Operating system loads program
โ
โ
main()
โ
โโโ declarations
โ
โโโ statements
โ
โโโ function calls
โ
โโโ return
โ
โ
Program termination
The source file is read and translated before normal execution. During execution, the running program follows its control flow starting from the program's entry point.
The semicolon ; is used to terminate many C statements.
int x = 10;
x = x + 5;
printf("%d\n", x);
return 0;
For example, a function definition does not end with a semicolon after its closing brace:
void greet(void)
{
printf("Hello");
}
Similarly, the if, for and while constructs have
their own syntax rules.
Spaces, tabs and newlines are generally used to make source code readable. C does not require a particular indentation style.
int main(void)
{
int a = 10;
int b = 20;
printf("%d\n", a + b);
return 0;
}
The indentation does not change the meaning of this simple program. It improves readability for humans.
- Use consistent indentation.
- Keep related statements visually grouped.
- Use meaningful names.
- Avoid unnecessarily long lines.
- Use comments to explain important reasoning.
C distinguishes between uppercase and lowercase letters.
int age = 20;
printf("%d", age);
These identifiers are different:
age
Age
AGE
aGe
Writing:
Printf("Hello");
instead of:
printf("Hello");
will not refer to the same identifier.
#include <stdio.h>
int add(int a, int b);
int main(void)
{
int x;
int y;
int result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
result = add(x, y);
printf("Sum = %d\n", result);
return 0;
}
int add(int a, int b)
{
return a + b;
}
| Question | Answer |
|---|---|
| Where is the standard I/O header included? | #include <stdio.h> |
| Where is the function declaration? | int add(int a, int b); |
| Where does the program begin? | main() |
| Where are local variables declared? | Inside main(). |
| Where does input happen? | scanf() |
| Where is the function called? | result = add(x, y); |
| Where is the function defined? | After main(). |
| What does add() return? | The sum of a and b. |
| Mistake | Problem | Correct idea |
|---|---|---|
Missing #include <stdio.h> |
Standard I/O declarations may be unavailable. | Include the appropriate header when required. |
Using void main() |
Not a standard hosted C form. | Use int main(void). |
| Missing semicolon | Many statements require termination. | Check the statement syntax. |
Missing } |
Function or block structure becomes incomplete. | Match braces carefully. |
| Calling a function without an appropriate visible declaration | The compiler may not have enough information to process the call correctly. | Declare the function before its use when needed. |
| Confusing header with library | Misunderstands preprocessing and linking. | Separate header declarations from library implementation/linking. |
| Putting executable code outside functions | Ordinary executable statements belong inside function bodies. | Place statements inside an appropriate function. |
| Confusion | Correct Understanding |
|---|---|
| โEvery C program must have exactly six sections.โ | The six-section model is a teaching convention, not a universal language requirement. |
| โ#include is a function.โ | #include is a preprocessor directive. |
| โstdio.h contains printf's executable code.โ | The header provides declarations and related information; implementation is supplied by the library/toolchain. |
| โreturn 0 prints zero.โ | return 0; returns a value from the function. |
| โmain() must always be written with empty parentheses.โ | int main(void) explicitly indicates no arguments. |
| โA function must always be defined before main().โ | A suitable declaration can allow its definition to appear later. |
| โAll declarations must always come before every statement.โ | Modern C permits declarations and statements to be arranged more flexibly within blocks, subject to the language rules. |
| โComments are executed.โ | Comments are ignored as comments during translation. |
| Traditional Teaching Model | Actual Meaning |
|---|---|
| Documentation section | Comments and program documentation. |
| Link section | Usually refers to header inclusion using #include. |
| Definition section | May include macros or other definitions. |
| Global declaration section | Declarations/definitions at file scope. |
| main() section | The program's entry function in hosted C. |
| Subprogram section | User-defined function definitions. |
If an interviewer asks about the โstructure of a C program,โ you can explain the traditional model, but also mention that it is a convenient organizational model, not a mandatory six-part template imposed on every C source file.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ COMMENTS โ โ What is this program? โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ PREPROCESSOR โ โ #include / #define โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ DECLARATIONS โ โ Types / functions / names โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ main() โ โ Program entry function โ โ โ โ โโโ local declarations โ โ โโโ input โ โ โโโ processing โ โ โโโ function calls โ โ โโโ output โ โ โโโ return โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ OTHER FUNCTION DEFINITIONSโ โ Reusable program logic โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.26 Quick Revision
- A C source file contains different language elements rather than one rigid template.
#includeis a preprocessor directive.main()is the program entry function in a hosted C environment.- Declarations tell the compiler about identifiers and their types/properties.
- Statements describe actions or control operations.
- Functions divide programs into manageable units.
- A declaration and a definition are not always the same thing.
- Header inclusion and library linking are different stages.
- Good indentation improves readability.
- C is case-sensitive.
2.27 Practice Questions
main() function is the designated entry function of a hosted C program.
#include is a preprocessor directive that requests inclusion of a header's contents according to the preprocessing rules.
int main(void) is a standard hosted C form. void main() is not a standard hosted C form.
#include is processed before compilation. Linking is a later toolchain stage that resolves external references and combines required object/library components.
0 from main(). In a hosted environment, this indicates successful termination to the host environment.
age, Age and AGE are different identifiers.
๐ค Structure of C Program โ Interview Questions
These questions focus on the concepts interviewers commonly test when checking whether a candidate understands C program structure rather than simply memorizing syntax.
main()
and may contain additional user-defined functions.
#include is a preprocessing operation involving source/header inclusion.
Linking is a later stage where external references are resolved and object files
and required libraries are combined.
main return an integer status to the host environment.
๐ก Structure of C Program โ Placement Tips
- Be able to explain preprocessing, compilation, assembly and linking in simple language.
- Know the difference between a function declaration and a function definition.
-
Remember that
#includeis a preprocessor directive, not a function. -
Use
int main(void)when teaching a program with no command-line arguments. -
Never confuse
return 0;with printing zero. -
Understand why a function can be defined after
main()when a suitable declaration appears earlier. - Know the difference between a header and a library implementation.
- Do not memorize the traditional six-section structure as a mandatory C rule.
โ๏ธ Structure of C Program โ Extra Practice Questions
- Write a simple C program and identify every major component.
-
Explain the difference between
#includeand a function call. -
Write a program where a user-defined function is declared before
main()and defined aftermain(). - Find and correct three structural errors in a small C program.
- Draw the complete path: source โ preprocessing โ compilation โ linking โ execution.
-
Explain why
void main()should not be presented as standard hosted C. - Identify declarations, definitions, expressions and statements in a given program.
- Write a program containing two user-defined functions and explain the role of each.