All for Joomla All for Webmasters

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:

  1. #if expression
  2. //code
  3. #endif

Syntax with #else:

  1. #if expression
  2. //if code
  3. #else
  4. //else code
  5. #endif

Syntax with #elif and #else:

  1. #if expression
  2. //if code
  3. #elif expression
  4. //elif code
  5. #else
  6. //else code
  7. #endif

C #if example

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

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #define NUMBER 0
  4. void main() {
  5. #if NUMBER==0
  6. printf(“Value of Number is: %d”,NUMBER);
  7. #endif       
  8. getch();
  9. }

Output:

Value of Number is: 0
PinIt
submit to reddit

Leave a Reply

Top