Nano Technology

How C Program Works


1. Overview of C Program Execution

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.

Source Code (.c)

You write human-readable C code like printf("Hello");
The CPU can’t run this directly.

Preprocessing (#include, #define)

Expands headers and macros into your file.
Produces an expanded source ready for the compiler.

Compilation (Syntax Check → Assembly)

Checks syntax and converts C into assembly.
Outputs a .s assembly file.

Assembly → Object Code

Assembler translates assembly into machine code.
Generates an object file (.o / .obj).

Linking (Libraries & Objects)

Links object files with standard libraries.
Creates one complete program image.

Executable (.exe / .out)

The final runnable file produced by the linker.
Run using ./a.out or program.exe.

Source → Preprocess → Compile → Assemble → Link → Execute

2. Source Code and Compilation

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.

3. Preprocessing Stage

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:

  • Including header files using #include
  • Expanding macros defined using #define
  • Removing comments from the program
#include <stdio.h>
                    #define PI 3.14
                    

After preprocessing, an expanded source code is produced, which is then passed to the compiler.

4. Compilation Stage

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.

5. Assembly Stage

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.

6. Linking Stage

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.

7. Execution Stage

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.

8. Flow Diagram Explanation

The working of a C program can be summarized using the following sequence:

  • Source Code (.c)
  • Preprocessing
  • Compilation
  • Assembly
  • Linking
  • Executable File
  • Program Execution

This step-by-step transformation ensures that the high-level C program is converted into machine-understandable instructions.

9. Common Compilation Errors

While writing C programs, beginners often encounter compilation errors. Some common errors are:

  • Missing semicolon at the end of statements
  • Undeclared variables
  • Incorrect function names
  • Mismatched parentheses or braces
  • Missing header files

Carefully reading compiler error messages helps in identifying and correcting these errors.

10. Summary

  • A C program must be compiled before execution.
  • The execution involves preprocessing, compilation, assembly, linking, and execution.
  • Each stage has a specific role in program execution.
  • Understanding this process helps in debugging errors efficiently.
Note: A clear understanding of how a C program works is essential before learning advanced topics like pointers, file handling, and memory management.