Lesson 4
Compilation and Execution Processs
A C program must go through several stages before it can run: preprocessing, compilation, linking, and execution. Understanding this process helps identify and fix errors effectively.
C Track
Save progress as you learn.
44 lessons
Lesson content
Compilation and Execution Process in C Programming Explained
Before a C program can run on a computer, it must go through several stages known as the compilation and execution process. A C program written by a programmer cannot be understood directly by the computer because it is written in a human-readable language called source code. The compiler, linker, and operating system work together to convert the source code into an executable program that the computer can understand and execute.
What Is the Compilation and Execution Process in C?
- Source Code: The human-readable C program written by the programmer, saved with a .c extension.
- Compiler: A software tool that translates C source code into machine-readable object code.
- Linker: A tool that combines object files with library files to produce an executable program.
- Preprocessor: The first phase of compilation that handles directives beginning with the # symbol.
The compilation and execution of a C program involve the following stages:
- Source Code (.c) → Preprocessor
- Preprocessor → Compiler
- Compiler → Object File (.obj / .o)
- Object File → Linker
- Linker → Executable File (.exe / executable)
- Executable File → Execution
Files Used During the Compilation Process
Source Code File
The source code file contains the C program written by the programmer. It includes variable declarations, functions, statements, and other program logic. Source code files use the .c extension and are usually created using a text editor or an Integrated Development Environment (IDE).
- program.c
- student.c
- calculator.c
Header Files
Header files contain declarations of library functions, macros, constants, and data types that can be used in the source code. Header files use the .h extension. They can be built-in header files such as stdio.h, stdlib.h, and math.h, or user-defined header files created by programmers.
- Header File: A file containing predefined functions, macros, and declarations used in C programs.
Object File
After successful compilation, the compiler generates an object file. An object file contains machine code, but it is not yet executable because references to library functions and external code are not fully resolved.
- .obj - Windows
- .o - Linux/macOS
- Object File: A file containing machine code generated by the compiler, not yet fully executable.
Executable File
The linker combines one or more object files with the required library files to produce an executable file. The executable file contains complete machine code that can be executed directly by the operating system.
- .exe - Windows
- No extension or platform-specific formats - Linux and macOS
- Executable File: A file containing complete machine code that can be run directly by the operating system.
Steps Involved in Compiling and Executing a C Program
Step 1: Creating the Source Code
The first step is writing the C program using any text editor or IDE. The source file contains variables, functions, and program statements, must follow the syntax rules of C, and is saved with the .c extension.
Step 2: Compiling the Source Code
Compilation is the process of converting source code into machine-readable object code. The first phase of compilation is called preprocessing.
Preprocessing
During preprocessing, all lines beginning with the # symbol are processed. Common preprocessing tasks include:
- Including header files using #include
- Replacing symbolic constants defined using #define
- Expanding macros
- Removing comments
After preprocessing, the compiler checks the program for syntax errors. If no syntax errors are found, the compiler generates the object file. If syntax errors exist, the compilation stops and error messages are displayed for correction.
Step 3: Linking the Program
Once the object file is created, the linker combines it with the required library files. Many functions used in C programs, such as input/output and mathematical functions, are stored in standard libraries rather than in the source code itself.
During linking, the linker:
- Connects object files together
- Adds required library functions
- Resolves external references
- Produces the final executable file
- Linker: A tool that combines object files and library files to produce a final executable program.
Step 4: Executing the Program
After successful compilation and linking, the operating system loads the executable program into memory and begins execution. The program runs from the main() function, may ask the user to enter input, and the CPU executes each instruction sequentially. The desired output is displayed if no runtime errors occur.
If incorrect results are obtained, the programmer edits the source code, recompiles it, links it again, and executes it until the program behaves as expected.
What Is a Compiler?
A compiler is a software tool that translates a C program (source code) into machine code that the computer can understand and execute. In addition to translation, the compiler also checks the program for syntax errors and reports them to the programmer.
Popular C Compilers
- GCC (GNU Compiler Collection) - Open-source compiler widely used on Linux, macOS, and Windows
- Clang - Modern compiler known for fast compilation and detailed error messages
- Microsoft Visual Studio - Professional IDE with C/C++ support
- Dev-C++ - Free IDE and compiler for C and C++ programming
- Turbo C - One of the most popular compilers for learning C (primarily educational)
- Microsoft Quick C - Early compiler developed by Microsoft (primarily educational)
FAQ About Compilation and Execution in C
1. What is the compilation process in C programming?
The compilation process is the conversion of a C source program into machine-readable code. It includes preprocessing, compilation, linking, and execution.
2. What is the difference between source code and object code?
Source code is the human-readable C program written by the programmer, while object code is the machine-readable code generated by the compiler before linking.
3. What is the purpose of the linker?
The linker combines object files with the required library files, resolves external references, and creates the final executable program.
4. Why are header files used in C?
Header files contain declarations for library functions, macros, constants, and data types. They allow programmers to use built-in functions without rewriting them.
5. What happens if a C program contains syntax errors?
If syntax errors are present, the compiler stops the compilation process and displays error messages. The programmer must correct these errors before the program can be compiled successfully.
6. Which C compilers are commonly used today?
Some of the most popular C compilers are GCC (GNU Compiler Collection), Clang, Microsoft Visual Studio, Dev-C++, and Turbo C (primarily for educational purposes).