C++ language

adplus-dvertising
Pre Processors in C++
Previous Home Next

It is a program that processes our program before it is passed to compiler. Pre processor directives are special symbol.

Features of pre processor:

The pre processor offer several features called pre processor directives. Each of these pre processor directives begin with a # symbol. The directives can be placed anywhere in the program but are most often placed at the beginning of the program,before the first function definition.

We would learn the following pre processor directives here:

  1. Macro definitions
  2. File inclusion
  3. Conditional inclusions
  4. Miscellaneous directives.
  1. Macro definitions: In macros pre processor we use #define. The # define pre processor directive is use for symbolic constant.
  2. There are following pre defined macros:

    Macro Description
    __DATE__, This reprents date in the form of "MM DD YY".
    __TIME__, This reprents time in the form of "hour:min:sec".
    __LINE__, This represents the current line number of the source code..
    __FILE__, This represents the current file name.
    // Syntex:
    #define identifier replacement
    
    //Eg :
    #define UPPER 45
    main( )
    {
    int i ;
    for ( i = 1 ; i <= UPPER ; i++ )
    Cout << i ;
    }
    

    In this program instead of writing 25 in the for loop we are writing it in the form of UPPER, which has already been defined before main( ) through the statement,#define UPPER 25 This statement is called ‘macro definition’ or more commonly, just a ‘macro’. What purpose does it serve? During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25.

    When we compile the program, before the source code passes to the compiler it is examined by the C preprocessor for any macro definitions. When it sees the #define directive, it goes through the entire program in search of the macro templates; wherever it finds one, it replaces the macro template with the appropriate macro expansion.

    In C++ programming it is customary to use capital letters for macro template. This makes it easy for programmers to pick out all the macro templates when reading through the program.

    Note that a macro template and its macro expansion are separated by blanks or tabs. A space between # and define is optional. Remember that a macro definition is never to be terminated by a semicolon.

    Macros with arguments:

    The macros that we have used so far are called simple macros. Macros can have arguments, just as functions can. Here is an example that illustrates this fact.

    #define Area) ( 3.14 * x * x )
    main( )
    {
    float r1 = 9.35, r2 = 1.46, a ;
    a = AREA ( r1 ) ;
    Cout << "area of circle:" a  ;
    a = AREA ( r2 ) ;
    Cout << "area of circle:"  a  ;
    }
    
    Output
    area of circle: 274.50
    area of circle: 6.69

    In this program wherever the pre-processor finds the phrase Area) it expands it into the statement ( 3.14 * x * x ). However, that’s not all that it does. The x in the macro template Area) is an argument that matches the x in the macro expansion ( 3.14 * x * x ). The statement AREA(r1) in the program causes the variable r1 to be substituted for x. Thus the statement AREA(r1) is equivalent to: ( 3.14 * r1 * r1 ) After the above source code has passed through the pre-processor, what the compiler gets to work on will be this:

    main( )
    {
    float r1 = 9.35, r2 = 1.46, a ;
    a = 3.14 * r1 *r1 ;
    Cout << "Area of circle:" a  ;
    a = 3.14 *r2 * r2 ;
    Cut << "Area of circle:"  a  ;
    }
    

    Here is another example of macros with arguments:

    #define ISDIGIT(y) ( y >= 21 && y <= 31 )
    main( )
    {
    char ch ;
    Cout << "Enter any digit " ;
    Cin >> ch ;
    if ( ISDIGIT ( ch ) )
    Cout << "You entered a digit"  ;
    else
    Cout <<  "Illegal input" ;
    }
    

    Macros vs Function:

    1. In a macro call the pre-processor replaces the macro template with its macro expansion, in a stupid, unthinking, literal way. As against this, in a function call the control is passed to a function along with certain arguments, some calculations are performed in the function and a useful value is returned back from the function.
    2. Usually macros make the program run faster but increase the program size, whereas functions make the program smaller and compact.

      If we use a macro hundred times in a program, the macro expansion goes into our source code at hundred different places, thus increasing the program size. On the other hand, if a function is used, then even if it is called from hundred different places in the program, it would take the same amount of space in the program.

    3. File inclusion: The second pre-processor directive we’ll explore in this chapter is file inclusion. This directive causes one file to be included in another. The pre-processor command for file inclusion looks like this:
    4. #include "filename"

      If we have a very large program, the code is best divided into several different files, each containing a set of related functions. It is a good programming practice to keep different sections of a large program separate. These files are #included at the beginning of main program file.

    5. Conditional inclusion:We can, if we want, have the compiler skip over part of a source code by inserting the preprocessing commands #ifdef and #endif,which have the general form:
    6. #ifdef macroname
      statement 1 ;
      statement 2 ;
      statement 3 ;
      #endif
      

      If macroname has been #defined, the block of code will be processed as usual; otherwise not.

      #if and elseif directives:

      The #if directive can be used to test whether an expression evaluates to a nonzero value or not. If the result of the expression is nonzero, then subsequent lines upto a #else, #elif or #endif are compiled, otherwise they are skipped.

      A simple example of #if directive is shown below:

      main( )
      {
      #if TEST <= 5
      statement 1 ;
      statement 2 ;
      statement 3 ;
      #else
      statement 4 ;
      statement 5 ;
      statement 6 ;
      #endif
      

      If the expression, TEST <= 5 evaluates to true then statements 1, 2 and 3 are compiled otherwise statements 4, 5 and 6 are compiled. In place of the expression TEST <= 5 other expressions like ( LEVEL == HIGH || LEVEL == LOW ) or ADAPTER == CGA can also be used.

      program using #elif directive can be rewritten as shown below.

      #if ADAPTER == VGA
      code for video graphics array
      #elif ADAPTER == SVGA
      code for super video graphics array
      #else
      code for extended graphics adapter
      #endif
      
    7. Misllaneous directives: There are two more preprocessor directives available, though they are not very commonly used. They are:
      • #undef
      • ) #pragm
      • #undef: On some occasions it may be desirable to cause a defined name to become ‘undefined’. This can be accomplished by means of the #undef directive. In order to undefine a macro that has been earlier #defined, the directive, #undef macro template can be used. Thus the statement, #undef PENTIUM would cause the definition of PENTIUM to be removed from the system. All subsequent #ifdef PENTIUM statements would evaluate to false.
      • #pragma: This directive is another special-purpose directive that you can use to turn on or off certain features. Pragmas vary from one compiler to another. There are certain pragmas available with Microsoft C compiler that deal with formatting source listings and placing comments in the object file generated by the compiler.
    Previous Home Next