C #if

The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed.

Syntax:

snippet
#if expression
//code
#endif

Syntax with #else:

snippet
#if expression
//if code
#else
//else code
#endif

Syntax with #elif and #else:

snippet
#if expression
//if code
#elif expression
//elif code
#else
//else code
#endif

C #if example

Let's see a simple example to use #if preprocessor directive.

snippet
#include 
#include 
#define NUMBER 0
void main() {
#if (NUMBER==0)
printf("Value of Number is: %d",NUMBER);
#endif       
getch();
}

Output:

Output
Value of Number is: 0

Let's see another example to understand the #if directive clearly.

snippet
#include   
#include   
#define NUMBER 1
void main() {
clrscr();
#if (NUMBER==0)
printf("1 Value of Number is: %d",NUMBER);
#endif

#if (NUMBER==1)
printf("2 Value of Number is: %d",NUMBER);
#endif
getch();
}

Output:

Output
2 Value of Number is: 1
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +