How does a C program executes?

Here we will see how C programs are executed in a system. This is basically the compilation process of a C program from source code to executable machine code.

The following diagram shows how a C source code file gets converted into an executable program −

C Source Code (.c file) Preprocessor (.i file) Compiler (.s file) Assembler (.o/.obj file) Linker (.exe/.out file) Library Files Loader (Process in RAM) #include directives Assembly code Object code Executable Execution

Compilation Process Steps

The C program execution involves several distinct phases −

1. C Source Code

This is the original code written by the programmer in a .c file. Here's a simple example −

#include <stdio.h>
#define PI 3.14159

int main() {
    printf("Hello, World!<br>");
    printf("Value of PI: %.2f<br>", PI);
    return 0;
}

2. Preprocessing

The preprocessor handles directives like #include, #define, and #ifdef. It replaces header file inclusions with actual file contents and performs macro substitutions. The output is an expanded .i file.

3. Compilation

The compiler takes the preprocessed code and converts it into assembly language code specific to the target processor. This creates a .s file containing human-readable assembly instructions.

4. Assembly

The assembler converts assembly language into machine code (object code). This creates a .o or .obj file containing binary instructions that the processor can understand, but it's not yet executable.

5. Linking

The linker combines object files with library functions (like printf from the C standard library) to create a complete executable file. This resolves external references and creates the final .exe or executable file.

6. Loading and Execution

The loader loads the executable file into memory (RAM) and starts execution. Once loaded and running, the program becomes a process.

Example Output

When the above C program is compiled and executed, the output will be −

Hello, World!
Value of PI: 3.14

Key Points

  • Each step transforms the code from one format to another
  • The process is automatic when using commands like gcc filename.c
  • Object files are not executable until linked with necessary libraries
  • A program becomes a process only when it's loaded and executing in memory

Conclusion

C program execution involves six distinct phases: preprocessing, compilation, assembly, linking, loading, and execution. Understanding this process helps developers debug compilation errors and optimize their programs effectively.

Updated on: 2026-03-15T10:30:38+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements