What are preprocessor directives in C?
As the name suggests, the preprocessor directives in C are actually a text processing tool or we can call it a text substitution tool. All the preprocessor directives begin with a hash symbol #. It pre-processes the program before the compilation of the program. The C preprocessor is used automatically by the C compiler to transform the program before compiling. A series of textual transformations are done on its input and after that, it will return your C code to the compiler. And after that, the compiler compiles the code. All the preprocessor directive names start with a hash # which means all the statements starting with a # will first go to the preprocessor and then get executed.
The C preprocessor (CPP) is not a part of the compiler but it's a step that executes before the source code is compiled. Preprocessor directives in C can appear anywhere in the program but they apply only to the remainder of the source file. The flow of code from compilation, to preprocessor tool to final execution, is shown in the below diagram.
There are Mainly four Types of Preprocessor Directives:-
- Macros: The Macros directives replace every occurrence of the identifier with a predefined string which means we are substituting the identifier with a string. Its syntax is as follows:
let us see an example:
Output:
Explanation: In the above example, the value of PI that is (3.1415 approx) is assigned to the string PI.
- File Inclusion: A file inclusion directive is an external file containing function macro definitions, that can be included using #include directive. The name suggests you need to include any file at the start of the program. The syntax of this directive is as follows:
Let us see an example.
Explanation: In the above example, the header file <stdio.h> is used to include the functions like standard input and output functions.
- Conditional Compilation: This C preprocessor offers a feature that is known as conditional compilation which means it can be used to switch the ON or OFF of a particular line or group of lines in a program. The "ON" or "OFF" will work for only that line of code. Using it, we can skip any part of the code as per our needs. Some examples:
Let us see an example.
Output:
Explanation: In the above example, conditional directive #ifdef is used to apply the condition in the code, and #endif is used to terminate the code if the condition is satisfied.
- Line Control: This preprocessor directive in C provided information about the error or mistakes in a program. It gives the line number where there is an error in syntax, indentation, etc. Syntax as follows:
Let us see an example.
Output:
Explanation: In the above example, line control preprocessor directives #pragma is used to switch the control of line execution of code.
List of Preprocessor Directives in C:
All the preprocessor directives are explained below with suitable examples wherever necessary.
1. #include:
This directive is used to tell the system to include the current file specified in the input, in the rest of the original file. If the file that has to be included is not found, the compiler throws an error. This directive is of three types:
- #include
: This directive is used to include a header file. It searched the entire directory and finds that particular header file as specified by the user. - #include "file": This directive searched for the header file in the current directory. The directory of the current input file is called the current directory.
- #include anything else: If any of the above two cases failed, then this directive is used.
2. #define:
This preprocessor directive in C comes under Macros. A macro is one when some particular section of code is substituted by the value of the macro. There are two types of Macros:
- Object-like Macros: When the macro is replaced by a value, generally for numerical value. Example:- Here, "PI" is used with the preprocessor dictie. So the value of 'PI' = 3.1415 will be assigned in the code.
- Function-like Macros: When the macro id is replaced by a function call. Example:
3. #undef:
This directive is used to remove the definitions related to a particular macro. The syntax of this directive is as follows:
4. #ifdef:
This preprocessor directive is used to check if a macro is defined earlier or not. If it is defined, then the code executes normally. The syntax of this directive is as follows:
5. #ifndef:
This preprocessor directive is used to check whether a "#define" is defined or not defined by the user. If it is so, then the code will execute. The syntax of this directive is as follows:
6. #if:
This preprocessor directive is used for evaluating the conditions or expressions. If the condition satisfies, the code will execute. The syntax of this derivative is as follows:
7. #else:
This preprocessor directive is also used for the evaluation of conditions or expressions. But here it is used to check if the condition is false, then what will further happen to the flow of code like where the compiler will shift for the execution of the program. We can use it with #if, #ifdef, #elif, and #ifndef directives. The syntax of this directive is as follows:
8. #elif:
This directive can be used with the #if, #ifdef, or #ifndef directives and the #elif. If the condition is true then it provides more conditions for the program. The syntax of this directive is as follows:
9. #endif:
This preprocessor directive is used to indicate the end of the directives like #if, #ifdef, or #ifndef condition.
10. #error:
This preprocessor directive is used to show an error. The compiler shows an error directive and it immediately stops the further compilation of the program. The syntax of this directive is as follows:
11. #pragma:
This preprocessor directive is used to present some extra information to the compiler. This directive can provide information such as machine details or the details of the operating system to the compiler. The syntax of this directive is as follows:
Conclusion:
- Preprocessor directives in C are the simple text processing tool that starts with a hash #.
- All the codes before compilation, goes to the preprocessor for preprocessing.
- There are four types of derivatives in C that is Macros, File Inclusion, Conditional Compilation, and Line Control derivatives.
- Some part of the code is replaced by a name, known as Macros.