A C program does not run directly on the computer after it is written. The instructions written by the programmer are called source code, which must be converted into machine language before execution.
The complete execution of a C program involves a sequence of well-defined stages: preprocessing, compilation, assembly, linking, and execution. Each stage performs a specific task to transform the program into an executable file.
.c)You write human-readable C code like printf("Hello");
The CPU can’t run this directly.
#include, #define)Expands headers and macros into your file.
Produces an expanded source ready for the compiler.
Checks syntax and converts C into assembly.
Outputs a .s assembly file.
Assembler translates assembly into machine code.
Generates an object file (.o / .obj).
Links object files with standard libraries.
Creates one complete program image.
.exe / .out)The final runnable file produced by the linker.
Run using ./a.out or program.exe.
The C program written by the programmer is saved as a text file with the extension
.c. This file contains instructions written in a human-readable format.
Since the computer understands only machine language (binary code), the source code must be translated using a C compiler such as GCC. The compiler performs this translation step by step through different phases.
The first stage of C program execution is preprocessing.
It is handled by the preprocessor and deals with all statements that begin with #.
Major tasks performed during preprocessing include:
#include#define#include <stdio.h>
#define PI 3.14
After preprocessing, an expanded source code is produced, which is then passed to the compiler.
In the compilation stage, the compiler checks the program for syntax errors and translates the preprocessed code into assembly language.
If any grammatical errors are found in the program, the compiler displays error messages and stops the compilation process.
If the program is free from syntax errors, an assembly code file is generated.
In this stage, the assembly code generated by the compiler is converted into machine-level object code by a program called the assembler.
The output of this stage is an object file with extension .o or .obj.
This file contains machine instructions, but it is not yet executable.
The linking stage combines the object code with required
library functions such as printf() and scanf().
The linker resolves external references and produces a single executable file.
If required library files are missing, linking errors will occur.
In the execution stage, the final executable file is loaded into memory and executed by the operating system.
The program execution begins from the main() function,
and statements are executed sequentially until the program terminates.
The working of a C program can be summarized using the following sequence:
.c)This step-by-step transformation ensures that the high-level C program is converted into machine-understandable instructions.
While writing C programs, beginners often encounter compilation errors. Some common errors are:
Carefully reading compiler error messages helps in identifying and correcting these errors.