There are a lot of components or processors involved inside or outside the C compiler that affects the compilation process. One of them is the C preprocessor.
The preprocessor executes its own commands to the program before it is sent to the compiler for compilation. The preprocessor commands are called preprocessor directives.
Though the directives have nothing to do with C language, it has somehow become part of the C programming language.
Before you start learning about the C compiler, learn C programming basics.
Steps to Compile a C Program
To understand the role of the C preprocessor, we must first understand the process of compiling a C program successfully. The steps are listed below and each of the steps produces some output file.
Step 1: Write the Source code in the Text editor.
Output File: Prog.C
Step 3: The Prog.C to preprocessor and preprocessor expands its codes. The expanded source code is sent to C compiler.
Output File: Prog.I
Step 4: The Prog. I file is compiled and an intermediate object file is created if there is no error.
Output File: Prog.obj
Step 5: A linker links the object code of program and object code of library functions to a system and creates an executable code.
Output File: Prog.exe
To run the program, you need to execute the Prog.exe file.
Features of Preprocessor Directives
You can place a preprocessor directive anywhere within your program but the common practice is to place the code before the main function.
Every preprocessor directive starts with a pound sign (#) and then a directive name.
The next part in a preprocessor statement is called macro template and macro expansion.
#define SIDE 25
#define is called directive.
SIDE is called macro template
25 is the macro expansion
Types of Directives
The type of directives is basically its purpose. It is about what the macro does in the program. We can divide the preprocessor directive into the following categories.
- Macro Expansion
- File Inclusion
- Conditional Compilation
- Other misc directives
In future articles, we will learn about each one of them in more details.
- C Program to demonstrate usage of the #error directive.
- C Program to find the area of a square using a macro.