What is ifndef in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Ifndef is a condition inclusion of C++ programming language which is a type of directive in C++ and checks if a given macro is defined or not. The directive is a special instruction to the compiler provided by the user in C++ and the macro is a type of directive by which a part of the program (that could be a literal, variable, function, expression, etc.) can be given a name and the user can write that name instead of that code.

Condition inclusions are like conditional statements but for the macros. Ifndef can be broken as ‘if not defined’ and if a macro is not defined it can be detected by the Ifndef. Similar to the if-else statements, the code that is written next to Ifndef until a new condition inclusion came is included in the program compilation.

Syntax of #ifndef

The syntax of the #ifndef condition inclusion is:

In the above syntax, macro_name_one is the macro user who wants to check if it is defined or not. #elif is similar to the else-if condition and can be added any number of times including none. Also, macro_name_second can be equal to ‘macro_name_first’. In the end, #endif is present which is a must to end the condition inclusions and indicate no more code to compile from the ‘ifndef’ condition.

How does #ifndef Work in C++?

In C++ programming language, the user can provide special instructions to the compiler before the program is invoked using the preprocessor directives. There are many types of directives; two are the macro definition and condition inclusions.

A macro definition means defining a macro and a macro is a replacement for a piece of code in a program. When the macro is encountered by the compiler, the compiler replaces the macro with the piece of code for which it was defined or simply its definition.

For example, the value of pi is 3.141592653589 instead of putting this value in any local or global variable, we can define a macro PI (or any other name) to use that name through the code.

A macro can be defined using the #define directive and it can be undefined using the #undef directive. Now in a project, if a programmer has no idea whether the current directive is defined or not, then he/she cannot use it because of fear of compilation. To overcome this issue there are condition inclusions present and one of them is ifndef.

The code present in between "ifndef" and any one of the elif, else, or endif condition inclusion is processed to compilation if the macro written after "ifndef" is not defined. Also, adding ‘endif’ is a must at the end of condition inclusions to indicate the ending of the piece of code to compile if the macro is defined or not.

Examples

We have seen the basics of the ifndef and its syntax, now let's move to see some examples to understand its implementation.

Example1: In this example, we are going to define a macro and will try to use ifndef, then we will un-define it and again check using ifndef.

Code:

Output:

Explanation:

In the above code, first, we have defined the macro variable to literal 5. We checked if the variable macro is not defined by using ifndef and defined an else condition also. After that, we undefined the variable macro using the undef directive. In line 18, again, we have checked for the variable macro and as it was undefined so, it is not present, and the code under the condition #ifndef variable will be executed and the variable macro will be defined again with literal 10 that we are printing at the end of the main function.

Example2: In this example, we are going to define a macro that will represent the size of an array and will try to show the practical use of ifndef in real-life programs.

Code:

Explanation:

In the above code, first, we have defined the macro MAX_SIZE to literal 100 which will represent the maximum size of an array. Now, imagine in a large project a user wants to define the maximum size of the array to the different value and is not sure that it was defined above, then ifndef can be used. In line 17, we checked whether MAX_SIZE was defined or not, if not defined then we defined it to a literal value and if already defined, then changed it to the required value.

Conclusion

  • Ifndef is a condition inclusion of C++ programming language which is a type of directive in C++ and checks if a given macro is defined or not.
  • The directive is a special instruction to the compiler provided by the user in C++ and the macro is a type of directive by which a part of the program can be given a name.
  • Ifndef can be broken as ‘if not defined’ and if a macro is not defined it can be detected by the Ifndef.
  • The code present in between indef and any one of the elif, else, or endif condition inclusion is processed to compilation if the macro written after indef is not defined.
  • Adding ‘endif’ is a must at the end of condition inclusions to indicate the ending of the piece of code to compile if the macro is defined or not.