Preprocessors are programs that are not a part of the compiler but are used by the compiler to process the source code before compilation. It is a text substitution tool that instructs the compiler to do a required pre-processing before the actual compilation, it is a separate step in the compilation process.

Recommended Topic, Sum of Digits in C, C Static Function
What are preprocessor Directives in C?
Preprocessors in C are programs that provide preprocessors directives that tells the compiler to preprocess the source code before compiling. Preprocessor directives always begin with a “#” (hash) symbol. All the statements starting with the hash symbol go to the preprocessor program, and the preprocessor program will execute these statements. you can also use an online compiler editor.
Examples of preprocessor directives are #include, #define, etc.
List of preprocessor directives in C are mentioned below:
Directive | Description |
---|---|
#define | Defines macros or constants to be replaced before compilation. |
#undef | Undefines a previously defined macro. |
#include | Includes the contents of a specified file (header file) into the source file. |
#if | Starts a conditional compilation block based on a condition. |
#ifdef | Compiles the code that follows if the specified macro is defined. |
#ifndef | Compiles the code that follows if the specified macro is not defined. |
#else | Provides an alternative block of code if the condition in #if or #ifdef fails. |
#elif | Combines #else and #if to check another condition if the previous #if failed. |
#endif | Ends the conditional compilation block started with #if, #ifdef, or #ifndef. |
#error | Generates a compilation error with a custom message if reached. |
#pragma | Issues special commands to the compiler, often for controlling compiler behavior. |
#line | Changes the line number and filename for the following lines of code. |
Types of Preprocessor Directives
- Macros
- File Inclusion
- Conditional Compilation
- Other directives
1. Macros
A macro is a segment of code in a program that is given some name, when they are encountered in the code by the compiler, the compiler replaces the name with the actual piece of code, which is usually a value. We use “#define” directive to define macros. Macro definitions do not end with a semicolon.
Example of Macros
Output
Counting...
1
2
3
4
5
Explanation
In the above code, when the compiler reads the word “n” it replaces it with its value defined in the starting code. The word “n” is referred to as macro template, and its value “5” is referred to as macro expansion.
We can define macros with arguments that work the same way functions do.
Example
Output
Area of square is: 100
Explanation
In the above code, whenever the compiler finds area(side), it replaces it with the statement (side*side). The value passed as arguments is replaced with the variables, and the calculated value is displayed.
Macro | Description |
---|---|
__DATE__ | Set the current date as a character literal in "MMM DD YYYY" format. |
__TIME__ | Sets the current time as a character literal in "HH:MM:SS" format. |
__FILE__ | This contains the current filename as a string literal. |
__LINE__ | This contains the current line number as a decimal constant. |
__STDC__ | Defined as 1 when the compiler complies with the ANSI standard. |
Example
Output
File Name : test.c
Current Date : Mar 11 2022
Current Time : 12:27:45
Line : 8
ANSI : 1
2. File Inclusion
Whenever we want to include a file in the source code, we use the file inclusion type of preprocessor. Mainly two types of files can be added using this preprocessor:
Standard Header Files
Header files are the files that are added on the top of source code and contain pre-defined functions like printf(), scanf(), etc. Header files must be included to use these functions, different functions are declared in different header files.
Syntax
#include<file_name>
The angular brackets tell the compiler to look for the file with the mentioned file_name in the standard directory.
User Defined Header files
When working on a big project/program, the program is divided into smaller files that can be included whenever needed. This makes the program more readable and helps the user focus on a particular functionality.
Syntax
#include "file_name"
3. Conditional Compilation
These are the directives that enable the user to compile a certain set of instructions or skip specific instructions based on some condition. Conditional compilation directives are declared using two preprocessing commands: “ifdef” and “endif”.
Following preprocessor directives that are used to insert conditional code:
- #if Directive
- #ifdef Directive
- #ifndef Directive
- #else Directive
- #elif Directive
- #endif Directive
Syntax
#ifdef macro_name
// Code will execute if macro_name is defined
#ifndef macro_name
// Code will execute if macro_name is not defined
#if constant_expr
// Code will execute if constant_expr evaluates to true
#elif another_constant_expr
// Code will execute if another_constant_expr evaluates to true
#else
// Code will execute if none of the above conditions are true
#endif
Example
Output
Debugging is enabled.
Release mode is not enabled.
Version 2 of the software.
4. Other directives
There are two more directives that are not commonly used in this directives:
- #undef Directive
- #pragma Directive
1. #undef Directive
This is used to undefine an existing macro.
Example
Output
MAX is not defined.
Explanation
In the above code, we first define the MAX and then undefine it using the #undef. As a result, every “ifdef MAX” statement evaluates as false.
2. #pragma directive
This directive is used to turn on/off some features, these directives are compiler-specific. Some of the #pragma directives are:
#pragma startup & #pragma exit
These directives are used to specify which functions are needed to run before control passes to main() and just before the control returns from main(). These directives do not work with GCC compilers as they don't support #pragma startup/exit.
Example
Output
Before starting of main() function
Inside main()
Before ending of main() function()
Also see, Tribonacci Series and Short int in C Programming