Preprocessor directives: In almost every program we come across in C/C++, we see a few lines at the top of the program preceded by a hash (#) sign. These lines are preprocessed by the compiler before actual compilation begins. The end of these lines are identified by the newline character ‘\n’ , no semicolon ‘;’ is needed to terminate these lines.
Preprocessor directives are mostly used in defining macros, evaluating conditional statements, source file inclusion, pragma directive, line control, error detection etc.
In this post, we will discuss about some more types of preprocessor directives given below:
- Conditional Compilation
- Line control
- Error directive
Let us now have a look about each one of these directives in details:
Conditional Compilation: Conditional Compilation directives help to compile a specific portion of the program or let us skip compilation of some specific part of the program based on some conditions. In our previous article, we have discussed about two such directives ‘ifdef‘ and ‘endif‘. In this post we will discuss #ifndef, #if, #else and #elif.
#ifndef: We know that the in #ifdef directive if the macroname is defined, then the block of statements following the #ifdef directive will execute normally but if it is not defined, the compiler will simply skip this block of statements. The #ifndef directive is simply opposite to that of the #ifdef directive. In case of #ifndef , the block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined.
ifndef macro_name statement1; statement2; statement3; . . . statementN; endif
If the macro with name as ‘macroname‘ is not defined using the #define directive then only the block of statements will execute.
#if, #else and #elif: These directives works together and control compilation of portions of the program using some conditions. If the condition with the #if directive evaluates to a non zero value, then the group of line immediately after the #if directive will be executed otherwise if the condition with the #elif directive evaluates to a non zero value, then the group of line immediately after the #elif directive will be executed else the lines after #else directive will be executed.
#if macro_condition statements #elif macro_condition statements #else statements #endif
#define gfg 7
#if gfg > 200
#define gfg 200
#elif gfg < 50
#define gfg 50
#define gfg 100
std::cout << gfg;
// gfg = 50
Notice how the entire structure of #if, #elif and #else chained directives ends with #endif.
- #ifndef: We know that the in #ifdef directive if the macroname is defined, then the block of statements following the #ifdef directive will execute normally but if it is not defined, the compiler will simply skip this block of statements. The #ifndef directive is simply opposite to that of the #ifdef directive. In case of #ifndef , the block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined.
Line control ( #line ): Whenever we compile a program, there are chances of occurrence of some error in the program. Whenever compiler identifies error in the program it provides us with the filename in which error is found along with the list of lines and with the exact line numbers where the error is. This makes easy for us to find and rectify error.
However we can control what information should the compiler provide during errors in compilation using the #line directive.
#line number "filename"
number – line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on.
“filename” – optional parameter that allows to redefine the file name that will be shown.
- Error directive ( #error ): This directive aborts the compilation process when it is found in the program during compilation and produces an error which is optional and can be specified as a parameter.
Here, optional_error is any error specified by the user which will be shown when this derective is found in the program.
#error GeeksforGeeks not found !
error: #error GeeksforGeeks not found !
This article is contributed by Amartya Ranjan Saikia and Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- isgraph() C library function
- How to write your own header file in C?
- Continue Statement in C/C++
- How a Preprocessor works in C?
- Basic Input / Output in C++
- Operators in C / C++
- C/C++ Preprocessors
- typedef versus #define in C
- Arrays in C/C++
- Diffference between #define and const in C?
- Commonly Asked C++ Interview Questions | Set 1
- C Language Introduction
- Variable length arguments for Macros
- Do not use sizeof for array parameters
- Write a C macro PRINT(x) which prints x