Understanding the structure of a C program is one of the first important
steps in learning C programming. A C program is made up of different
parts such as header files, functions, statements, comments, and the
main() function.
Each part has a specific purpose. Once you understand these parts, it becomes much easier to read, write, and debug C programs.
main() function is the normal entry point of a hosted C
program.
A simple C program can look like this:
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
This small program contains a header file, the main()
function, a statement, and a return statement.
A typical C program may contain the following parts:
main() functionSome of these parts are optional depending on the requirements of the program.
Comments are used to add explanations or notes to source code. They are not treated as executable program instructions.
Single-line comment:
// This is a comment
Multi-line comment:
/*
This is a
multi-line comment
*/
Comments can make programs easier to understand and maintain.
Preprocessor directives begin with the # symbol.
For example:
#include <stdio.h>
The preprocessor processes directives before the compiler translates the main source code.
Common directives include #include and
#define.
Header files contain declarations and other information that can be shared with a C source file.
For example:
#include <stdio.h>
The stdio.h header is commonly used when a program needs
standard input and output functions.
The main() function is the normal entry point of a hosted
C program.
int main()
{
printf("Hello");
return 0;
}
When the program starts, execution begins in the main()
function.
A common and recommended form of the main function is:
int main()
{
return 0;
}
The int indicates that main() returns an
integer value to the environment that started the program.
The return statement ends a function and can provide a
value to the caller.
int main()
{
return 0;
}
Returning 0 from main() commonly indicates
successful program termination.
Statements are instructions that perform actions in a C program.
printf("Hello");
int age = 20;
age = age + 1;
Many C statements end with a semicolon.
The semicolon ; is used to terminate many C statements.
int age = 20;
printf("%d", age);
return 0;
Forgetting a required semicolon can result in a compilation error.
Curly braces { } define a block of code.
int main()
{
printf("Hello");
return 0;
}
The statements between the braces belong to the function body.
Indentation means using spaces or tabs to make the structure of code easier to read.
Example:
int main()
{
int age = 20;
if(age >= 18)
{
printf("Adult");
}
return 0;
}
Indentation does not normally change C program execution, but it greatly improves readability.
Variables can be declared inside a function. Such variables are local to that function.
int main()
{
int age = 20;
float marks = 85.5;
printf("%d", age);
return 0;
}
A local variable is declared inside a function or block.
int main()
{
int number = 10;
printf("%d", number);
return 0;
}
The variable number is local to the block in which it is
declared.
A global variable is declared outside all functions.
#include <stdio.h>
int count = 10;
int main()
{
printf("%d", count);
return 0;
}
Its scope can make it accessible to multiple functions in the same source file, subject to the language's scope and linkage rules.
A function declaration tells the compiler about a function before it is used.
#include <stdio.h>
int add(int, int);
int main()
{
int result;
result = add(10, 20);
printf("%d", result);
return 0;
}
int add(int a, int b)
{
return a + b;
}
The declaration allows the compiler to know the function's name, parameters, and return type.
A function definition contains the actual code that performs the function's task.
int add(int a, int b)
{
return a + b;
}
Here, the function calculates the sum of two integers and returns the result.
A function is called by writing its name followed by parentheses and any required arguments.
int result;
result = add(10, 20);
The program transfers control to the function and receives its return value.
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int sum;
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
This program contains a header file, the main function, variable declarations, an expression, an output statement, and a return statement.
A simple C program can be understood through this general flow:
Source Code
↓
Preprocessor
↓
Compiler
↓
Object Code
↓
Linker
↓
Executable Program
↓
Execution
The exact build process can vary depending on the compiler and environment.
During preprocessing, directives such as #include and
#define are handled.
#include <stdio.h>
#define PI 3.14
The preprocessor produces source code that is then passed to later compilation stages.
The compiler translates C source code into a lower-level form suitable for later stages of the build process.
If the source code contains syntax or other compile-time errors, the compiler can report them.
The linker combines compiled code with required libraries and other object files to create the final executable or program image.
For example, a program using standard library functions may need the appropriate runtime and library components during the linking stage.
After the program has been successfully built, the resulting executable can be run.
Build
↓
Executable
↓
Run Program
↓
Output
For example, a program containing printf() may display text
on the terminal.
A C program can accept input from the user.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}
Here, scanf() receives formatted input and stores it in the
variable.
#include <stdio.h>
int main()
{
int age;
printf("Enter age: ");
scanf("%d", &age);
if(age >= 18)
{
printf("Eligible");
}
else
{
printf("Not Eligible");
}
return 0;
}
This example demonstrates how variables, input, conditions, output, and the main function can work together.
Beginners commonly make mistakes such as:
printf("Hello")
The above statement is missing a semicolon.
For beginner programs, the following structure is a useful starting point:
#include <stdio.h>
int main()
{
// Variable declarations
// Program statements
// Output
return 0;
}
As you learn more C, you will add functions, structures, header files, and other components when needed.
#include <stdio.h>
int add(int a, int b);
void message();
int main()
{
int result;
message();
result = add(10, 20);
printf("Sum = %d", result);
return 0;
}
void message()
{
printf("Welcome to C\n");
}
int add(int a, int b)
{
return a + b;
}
This example demonstrates a program containing a header, function
declarations, main(), function calls, and user-defined
functions.
A C program is organized into different components that work together to perform a task.
Comments
↓
Preprocessor Directives
↓
Header Files
↓
Declarations
↓
main()
↓
Variables and Statements
↓
User-Defined Functions
The exact structure can vary from program to program. Small programs may contain only a few components, while larger applications can contain multiple source files, header files, structures, and many functions.
main() function is the normal entry point of a hosted C program.#include is commonly used to include header files.stdio.h provides declarations for standard input and output functions.Question: Which function is the normal entry point of a hosted C program?