#ifdef in C Programming Language

#ifdef in C


    1. if, #else and #elif: These directives works together and control compilation of portions of the program using some conditions. If the condition with the #if directive evaluates to a non zero value, then the group of line immediately after the #if directive will be executed otherwise if the condition with the #elif directive evaluates to a non zero value, then the group of line immediately after the #elif directive will be executed else the lines after #else directive will be executed.
      Syntax:
      #if macro_condition
         statements
      #elif macro_condition
         statements
      #else
         statements
      #endif
      

      Example:

       

       

       

      #include<iostream>

        

      #define gfg 7

         

      #if gfg > 200

         #undef gfg

         #define gfg 200

      #elif gfg < 50

         #undef gfg

         #define gfg 50

      #else

         #undef gfg

         #define gfg 100

      #endif

        

      int main()

      {

          std::cout << gfg;  // gfg = 50

      }    

      Output:

      50
      

      Notice how the entire structure of #if, #elif and #else chained directives ends with #endif.

  • Line control ( #line ): Whenever we compile a program, there are chances of occurrence of some error in the program. Whenever compiler identifies error in the program it provides us with the filename in which error is found along with the list of lines and with the exact line numbers where the error is. This makes easy for us to find and rectify error.
    However we can control what information should the compiler provide during errors in compilation using the #line directive.
    Syntax:
    #line number "filename"
    

    number – line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on.