C/C++ Preprocessor directives | Set 2

C/C++ Preprocessor directives basics

Preprocessor directives: In almost every program we come across in C/C++, we see a few lines at the top of the program preceded by a hash (#) sign. These lines are preprocessed by the compiler before actual compilation begins. The end of these lines are identified by the newline character ‘\n’ , no semicolon ‘;’ is needed to terminate these lines.
Preprocessor directives are mostly used in defining macros, evaluating conditional statements, source file inclusion, pragma directive, line control, error detection etc.

In this post, we will discuss about some more types of preprocessor directives given below:

  1. Conditional Compilation
  2. Line control
  3. Error directive

Let us now have a look about each one of these directives in details:

  • Conditional Compilation: Conditional Compilation directives help to compile a specific portion of the program or let us skip compilation of some specific part of the program based on some conditions. In our previous article, we have discussed about two such directives ‘ifdef‘ and ‘endif‘. In this post we will discuss #ifndef, #if, #else and #elif.

    1. #ifndef: We know that the in #ifdef directive if the macroname is defined, then the block of statements following the #ifdef directive will execute normally but if it is not defined, the compiler will simply skip this block of statements. The #ifndef directive is simply opposite to that of the #ifdef directive. In case of #ifndef , the block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined.
      Syntax :

      ifndef macro_name
          statement1;
          statement2;
          statement3;
          .
          .
          .
          statementN;
      endif
      

      If the macro with name as ‘macroname‘ is not defined using the #define directive then only the block of statements will execute.

    2. #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.
    “filename” – optional parameter that allows to redefine the file name that will be shown.

    You can clearly see in the above program the filename is shown as “Syntax ERRRRRRROOOOOORRRRRR” and line 6.

  • Error directive ( #error ): This directive aborts the compilation process when it is found in the program during compilation and produces an error which is optional and can be specified as a parameter.
    Syntax:

    #error optional_error
    

    Here, optional_error is any error specified by the user which will be shown when this derective is found in the program.
    Example:

    #ifndef GeeksforGeeks
    #error GeeksforGeeks not found !
    #endif  
    

    Output:

    error: #error GeeksforGeeks not found !
    

References:

This article is contributed by Amartya Ranjan Saikia and Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.