๐๏ธ 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.
Use this lesson to recognise those parts in a new program and explain their purpose.
A C program contains parts such as declarations, functions, and statements. Each part has a job, and the C language has rules for how these parts fit together.
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 starting function of a hosted C program, and statements inside a function describe the actions it performs. Here, hosted means the usual environment with operating-system and C library support.
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 explain the code to readers. The compiler does not turn the words inside a comment into instructions to execute.
| 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;
}
Writing a C statement inside a comment does not run that statement. 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 supplies function declarations, macros, and types that a source file needs. A declaration tells the compiler what a name refers to.
#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. |
A header tells the compiler about facilities you can use. The compiled implementation of a library function is normally supplied separately during the build.
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 uses declarations to check names and types. For example, it needs to know that a variable is an int before checking arithmetic that uses it.
A statement describes an action or controls which action happens next. Assignment, if, loops, and return are examples you will study in this course.
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 describes a value or calculation. Its operands are the values it works with, and its operators specify actions such as addition or multiplication.
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 runs a function using the arguments given in the call. Arguments supply values the function needs.
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. In a hosted environment, returning from main ends the program and reports a status to the environment that ran it. Zero reports success. It does not add a zero to the displayed output.
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
Avoid placing every task in a large program inside main(). Divide the work into functions with clear tasks.
#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. |
You will use this distinction when working with functions, global variables, and programs split across 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.
Separate the order in which code is written from the order in which its statements run.
Source code
โ
โ
Preprocessing
โ
โ
Compilation
โ
โ
Object / executable generation
โ
โ
Operating system loads program
โ
โ
main()
โ
โโโ declarations
โ
โโโ statements
โ
โโโ function calls
โ
โโโ return
โ
โ
Program termination
The build tools translate the source first. When you run the program, execution enters main and follows statements, branches, loops, and function calls. A function written after main can run when main calls it.
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 Program Exit Status and main(void)
In a hosted C implementation, the entry point is main. Writing int main(void) states explicitly that no arguments are accepted. A return value of 0 indicates successful completion; EXIT_SUCCESS and EXIT_FAILURE from stdlib.h are portable named status values.
Complete C program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("Program completed successfully");
return EXIT_SUCCESS;
}
Expected output:
Program completed successfully
Step-by-step explanation:
- main begins execution in a hosted environment.
- puts prints a message and a newline.
- Returning EXIT_SUCCESS communicates success to the calling environment.
2.27 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.28 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.