About #if preprocessor directive in C language

#if preprocessor directive is used to execute some  code depending on a condition. If the condition is true then it will execute the code otherwise the compiler will go to the #elseif or #else and then #endif.

In this article we will learn about use of #if preprocessor in C. Here is the syntax of this preprocessor directive which we can use in any case.

// syntax of #if preprocessor directive

#if condition
  // code to execute

#elseif condition
  //code to execute

#else
  // code to execute

#endif

Now, let’s see a C program to learn use of #if.

C program using #if preprocessor directive

The program bellow will print the sting inside the #if block because we have defined the value of SYSTEM is 1. If you change the value into zero then you will see the difference.

// C program using #if preprocessor directive

#include <stdio.h>
#define SYSTEM 1

int main(){

  printf("Competitiveprogrammer.com is ");

  #if SYSTEM
  printf("best place ");   // this will printed if SYSTEM is true
  #endif

  printf("for learning C programming.\n");
  return 0;
}

Output of this program :

if-preprocessor-directive-in-c-c-program-using-if-directive