Updated March 28, 2023
Introduction to #Ifdef in C
#ifdef is a directive in the C programming language that permits conditional compilation. Here, the preprocessor checks if the mentioned macro exists before including the next code in the process of compilation. Unlike other directives present in the C language, this directive is the easiest and simplest one. The block used here is called a conditional group. Only if the name of the macro is defined, the controlled text gets involved in the output of the preprocessor output. The text which is controlled within a condition will hold preprocessing directives. These are executed only if the condition gets satisfied. Let us look into it in the following sections.
Syntax
Below is the syntax of the directive #ifdef in the C programming language.
#ifdef MACRO
//controlled text
#endif /* name_of_macro */
Below is the syntax of #ifdef in the C programming language that has an else part as well.
#ifdef MACRO
// code…
#else
//code of else….
#endif
The definition of the macro has to be defined for the preprocessor for including the source code of C into the application which is compiled. As mentioned in the above syntax, the #ifdef directive has to be closed with an #endif directive.
How #Ifdef Work in C?
First, create a #ifdef part inside the main method. Then, add an else part based on the requirement. Finally, use #endif once the block is finished. This helps in identifying whether the code is complete or not.
Rules that have to be followed while creating code using #ifdef directive:
- The NAME OF MACRO within the condition can contain preprocessing directives.
- Code executes only if the condition succeeds.
- Nesting of conditional groups can be done within other conditional groups.
- If the macro is defined, the condition gets to succeed. Otherwise, it will fail.
- A conditional group cannot be started in one file and end in another file.
Examples of #Ifdef in C
Let us see some sample programs on the #ifdef directive:
Example #1
C program that prints the value defined in # defines using #ifdef directive. (if part only present)
Code:
#include <stdio.h>
#define AGE 32
int main()
{
//executes if AGE is present
#ifdef AGE
printf("I am %d years old.\n", AGE);
#endif
printf("This is a sample program on #ifdef directive...\n");
return 0;
}
Output:
In this program, a constant AGE is defined as 32 with the help of #define. Inside the main method, #ifdef AGE is used, and it gets executed as a value is present for the AGE. An extra line outside the loop also gets printed on executing the code.
Example #2
C program that prints the value defined in # defines using #ifdef directive. (if and else are present)
Code:
#include <stdio.h>
#define AGE 32
int main()
{
//executes if AGE is present
#ifdef AGE
printf("I am %d years old.\n", AGE);
//executes if AGE is not present
#else
printf("Your age is not as expected..\n");
#endif
printf("This is a sample program on #ifdef directive...\n");
return 0;
}
Output:
In this program also, a constant AGE is defined as 32 with the help of #define. Inside the main method, #ifdef AGE is used, and it gets executed as a value is present for the AGE. Unlike the above program, an else part is also present in this code. An extra line outside the loop also gets printed on executing the code.
Example #3
C program that implements #ifdef directive. (if and else are present)
Code:
#include <stdio.h>
//#define AGE 32
int main()
{
//executes if AGE is present
#ifdef AGE
printf("I am %d years old.\n", AGE);
//executes if AGE is present
#else
printf("Your age is not as expected..\n");
#endif
printf("This is a sample program on #ifdef directive...\n");
return 0;
}
Output:
In this program also, a constant AGE is defined as 32 with the help of #define. But, unlike the above programs, that line is commented, which means AGE is not present in this code. Inside the main method, #ifdef AGE is used, and an else part is also used. As there is no value for AGE, else part gets executed. An extra line outside the loop also gets printed on executing the code.
Example #4
C program that displays today’s date using #ifdef directive. (if and else are present)
Code:
#include<stdio.h>
int main(){
#ifdef __DATE__
printf("Todays date is : %s",__DATE__);
#else
printf("Sorry, the data is not defined");
#endif
return 0;
}
Output:
In this program, a global identifier DATE defined in the header file <stdio.h> is used to get today’s date. Inside the main method, #ifdef DATE is used, and it gets executed. On executing the code, today’s date gets printed.
Example #5
C program that uses NOINPUT along with #ifdef directive.
Code:
#include <stdio.h>
#include <conio.h>
#define NOINPUT
void main()
{
int n=8;
//no need of input
#ifdef NOINPUT
n=7;
#else
printf("Enter the value that has to be assigned to n:");
scanf("%d", &n);
#endif
printf("The value of n is : %d\n", n);
getch();
}
Output:
In this program, NOINPUT is defined in the #define the part. Inside the main method, #ifdef and else part are present. As NOINPUT is present, there is no need to enter a value. That means if part gets executed. On executing the code, the value of n inside the if part gets printed.
Example #6
C program that comments NOINPUT along with #ifdef directive.
Code:
#include <stdio.h>
#include <conio.h>
//#define NOINPUT
void main()
{
int n=8;
//no need of input
#ifdef NOINPUT
n=7;
#else
printf("Enter the value that has to be assigned to n: ");
scanf("%d", &n);
#endif
printf("The value of n is : %d\n", n);
getch();
}
Output:
In this program, even though NOINPUT is defined in the #define part, it is commented. As NOINPUT is absent, the value has to be entered. That means, else part gets executed. On executing the code, the value of n has to be given as input, and it gets printed.
Conclusion
#ifdef is a directive in the C programming language that permits conditional compilation. In this article, different aspects such as syntax, working, and examples of #ifdef are explained in detail.
Recommended Articles
This is a guide to #Ifdef in C. Here we discuss How #Ifdef Work in C with Syntax and Examples along with codes and outputs. You may also have a look at the following articles to learn more –