Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Syntax
1.2.
Syntax with #else
2.
Syntax for #ifdef directive 
3.
Example 1
3.1.
Code
3.2.
Explanation
3.3.
Output
4.
Example 2
4.1.
Code
4.2.
Explanation
4.3.
Output
5.
Frequently Asked Questions
5.1.
What are the different types of preprocessors?
5.2.
Why are preprocessor directives?
5.3.
Why is the #else directive used?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C #ifdef

Author Ayushi Poddar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In the C programming language, the #ifndef directive helps us allow the conditional compilation. The preprocessor of C programming helps determine if the macro provided has not existed before, including the specific subsequent code in the C compilation process. #ifdef preprocessor checks whether the particular macro is not defined with the help of the #define directive. Now, if the condition is TRUE, it will help to execute the code, and if the state is FALSE, the else code of the #ifdef will be compiled or executed.

Syntax

#ifdef MACRO  
// code  inside
#endif  

Syntax with #else

#ifdef MACRO  
//successful code will get executed 
#else  
// else code will get executed
#endif 

Syntax for #ifdef directive 

#ifdef MACRO
//Code Statements
#else
//Code Statements which are used to include if the specific token is defined
#endif

Certain essential things related to the parameters mentioned in the syntax above are:

  • #ifdef MACRO - We must not define the directive if the preprocessor wants to include the source code during the compilation.
  • #else directive - In case the #ifdef directive does not accept, then else code statements will be printed which are actually used while including the specific which is defined.
  • #endif directive - The #ifdef should permanently be closed by an #endif directive. Otherwise, an error shall be encountered.

Example 1

Code

#include <conio.h>
#include <stdio.h>
#define INPUT
void main() {
int b1=0;
#ifndef INPUT
b1=2;
#else
printf("Enter b1 value :: ");
scanf("%d", &b1);
#endif
printf("The value of b1 :: %d\n", b1);
}

Explanation

Here we firstly load a few of the libraries like “conio.h” and “stdio.h”  are used, then we define the directive to be used as the MACRO value as INPUT. Then we define the main function, where the #ifdef preprocessor is used with the macro definition as INPUT, and then b1 variable value is stored with the value “2”  and then the else directive.

Output

Enter b1 value :: 10
The value of b1 :: 10
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example 2

Code

#include <conio.h>
#include <stdio.h>
void main() 
{
int b1=5;
int b2=1;
#ifndef INPUT
b1=3;
#else
printf("Enter int b1 value :: ");
scanf("%d", &b1);
printf("Enter int b2 value :: ");
scanf("%d", &b2);
#endif
printf("The value of b1 :: %d\n", b1);
printf("The value of b2 :: %d\n", b2);
int b3=b1+b2;
printf("The Sum of b1 and b2 :: %d\n", b3);
}

Explanation

Here we first load a few libraries like “conio.h” and “stdio.h”  are used. Then we define the main function, where the #ifdef preprocessor is used with the macro definition as INPUT. Then b1 variable value is stored with the value “3”  and then the else directive, and all this is inside the main function. The #ifdef directive is closed by the use of #endif.

Output

The value of b1 :: 5 
The value of b2 :: 2
The Sum of b1 and b2 ::  7

You can also read about dynamic array in c, Short int in C Programming and Tribonacci Series

Frequently Asked Questions

What are the different types of preprocessors?

There are 4 categories: macros, file inclusion, conditional compilation, and other directives.

Why are preprocessor directives?

All the commands used for the preprocessor are known as preprocessor directives, and all preprocessor directives are defined using #.

Why is the #else directive used?

In case the #ifdef directive does not accept, then else code statements will be printed which are actually used while including the specific which is defined.

Conclusion

In this article, we have extensively about the C #ifdef directive. We hope that this blog has helped you enhance your knowledge regarding #if directive in C language, and if you would like to learn more, check out our articles here. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
C #undef
Next article
C #ifndef
Live masterclass