Updated April 14, 2023
Introduction to #ifndef in C
The #ifndef directive of the C Programming Language helps in allowing the conditional compilation. The C Programming Language’s preprocessor helps in determining only if the macro provided is not at all existed before including the specific subsequent code in the C compilation process. The #ifndef preprocessor only checks If the specific macro is not at all defined with the help of the #define directive. If the condition is TRUE then it will be helpful in executing the code otherwise the else code of the #ifndef will be compiled or executed only if present.
Syntax:
#ifndef MACRO
//Code Statements
#else
//Code Statements which are used to include if the specific token is defined
#endif
Explanation of #ifndef in C syntax
- #ifndef MACRO: The #ifndef works for the opposite condition of the #ifdef directive of the C Programming Language. The “MACRO” definition should not be defined for the specific preprocessor which is used to include the C Programming Source Code into the specific compiled application. The #ifndef must be ended with the #endif directive of the C Programming Language.
- #else directive: If the #ifndef does not accept then else code statements will be printed which are actually used in including the specific which is defined.
- #endif directive: The #endif directive of the C Programming Language helps in closing the #ifndef directive of the C Programming Language. It is must and should end only with the #endif C Source code directive.
How #ifndef Directive Works in C?
The $ifndef directive usually checks/helps in seeing the specific identifier is currently not defined or not. The #ifndef preprocessor of the C Programming Language helps in allowing the conditional compilations. The preprocessor directive helps in determining whether the macro is existed or not before the subsequent code in the compilation process/ procedure.
The #ifndef directive and #if !defined identifier are equivalent directives of the C Programming Language. The #ifndef directive helps in checking the opposite condition of the #ifdef directive of the C Programming Language. If the specified identifier is not even defined or definition is removed with the help of the #undef then the condition is TRUE for nonzero value or else the condition will be FALSE.
Examples to Implement #ifndef in C
Below are the examples of #ifndef in C:
Example #1
This is an example of implementing the #ifndef preprocessor directive of the C Programming Language. Here at first some libraries of C language “conio.h” and “stdio.h” are used then #define directive is used with the MACRO value as INPUT. Then the main() function is created. Inside of the main() int variable a1 is created with the value “0” and then #ifndef preprocessor directive is used with the macro definition as INPUT and then a1 variable value is stored with the value “2” and then #else directive is used in order to get the input from the user. Then #endif directive of the C language is used to end the $ifndef directive. Then the value of the a1 is printed with the help of the printf() function. Check out the output so that you will know what is happened.
Code:
#include <conio.h>
#include <stdio.h>
#define INPUT
void main() {
int a1=0;
#ifndef INPUT
a1=2;
#else
printf("Enter a1 value :: ");
scanf("%d", &a1);
#endif
printf("The value of a1 :: %d\n", a1);
}
Output:
Example #2
This is also an example of implementing the #ifndef preprocessor directive functionality without the #define functionality to know what will happen if the #define functionality is not used. Here at first some key libraries “conio.h” and ”stdio.h” are included. Then the main() function is created. Inside the main() function an int variable value “0” is created. Then #ifndef directive is used with the MACRO definition as input and the an int value will “2” is stored to the a1 variable. Then #else derivative is used in order to get the input for the a1 variable. Then #endif directive of the c programming language is used which actually helps in ending the #ifndef preprocessor directive of the C Programming Language. Here the value “2” will be printed else conditions are not all be executed. Check out the output so that you will understand how the #ifndef worked if the #define is not at all used.5
Code:
#include <conio.h>
#include <stdio.h>
void main() {
int a1=0;
#ifndef INPUT
a1=2;
#else
printf("Enter a1 value :: ");
scanf("%d", &a1);
#endif
printf("The value of a1 :: %d\n", a1);
}
Output:
Example #3
This is also an example of implementing the #ifndef directive functionality of the C Programming Language. Here at first “stdio.h” library is included to include all the functions of the standard library of the C Programming Language. Then #define is used to define the age with the MACRO definition “YEARS_OLD” then #ifndef with MACRO definition is used and then again #define is used with different input and the whole is ended with the #endif directive of the C Programming Language. Here the value will be taken which is defined at first. After that even though if we pass a different value but that value will not at all be taken. Then inside of the main() function with the help of the printf() function the value of the YEARS_OLD will be printed. Here “13” will be printed. Check out the output below so that you can understand #ifndef concept better.
Code:
/* The Example of #ifndef directive of C Programming Language by Educba.com */
#include <stdio.h>
#define YEARS_OLD 13
#ifndef YEARS_OLD
#define YEARS_OLD 11
#endif
intmain()
{
printf("Educba.Com is over %d years old.\n", YEARS_OLD);
return 0;
}
Output:
Example #4
An example that is similar to the example 1 but here I used two int variables to produce the sum of those variable values. Here I used only one int variable inside the #ifndef so only a1 variable value will be considered as a1 value but not “5”. Then a2 value “1” will be added to the a1 value which is in the #ifndef directive. So the sum will be 4 instead of 6. Check out the output below so that you can understand better.
Code:
#include <conio.h>
#include <stdio.h>
void main() {
int a1=5;
int a2=1;
#ifndef INPUT
a1=3;
#else
printf"Enterint a1 value :: ");
scanf("%d", &a1);
printf"Enterint a2 value :: ");
scanf("%d", &a2);
#endif
printf("The value of a1 :: %d\n", a1);
printf("The value of a2 :: %d\n", a2);
int a3=a1+a2;
printf("The Sum of a1 and a2 :: %d\n", a3);
}
Output:
Conclusion
I hope you learnt what is the definition of #ifndef in C Programming Language along with its syntax and explanation, How the #ifndef directive works in C Programming Language along with its various examples to understand the #ifndef concept better and so easily.
Recommended Articles
This is a guide to #ifndef in C. Here we discuss the Introduction of #ifndef in C and how it works along with different Examples and its Code Implementation. You can also go through our other suggested articles to learn more –