What is the #if directive in C?

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;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved