Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is the #if directive in C?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The #if direction is a preprocessor used to easily evaluate if-else conditions in the C language. It is the same as the if condition that is widely used in C programming .

#if (expression)  
// given that expression is true, if is executed. 
//code  
#endif  
#if expression  
//if code  

// instead of else if, we use elif in this directive. 
#elif expression
//elif code  


#else  
//else code

#endif  

Code

Let’s look at a simple coding example:

widget
#include <stdio.h>
#define NUMBER -2
int main() {
#if (NUMBER>0)
printf("Value of Number greater than 0 is: %d",NUMBER);
#else
printf("Value of Number less than 0 is: %d",NUMBER);
#endif
return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring