Pages

PREPROCESSING UNIT 8 PART 3


#line
Introduction
The #line directive allows you to define arbitrary line numbers for the source lines. Normally, the compiler counts lines starting at line number 1; using the #line directive, you can specify an arbitrary line number at any point. The compiler then uses that line number for subsequent counts.
Program
#include <stdio.h>
main()
{
    printf("A\n");          //A

     #line100               //H
    printf("B\n");          //B
    printf("C FILE %s LINE %d\n", __FILE__, __LINE__ );//C
     #line200               //K

    printf("D\n");          //D
    printf("E\n");          //E
}
Explanation
  1. The statement H indicates the #line directive.
  2. The #line number in statement B is taken as 100 and for statement C, it is taken as 101.
  3. The #line number in statement D is taken as 200 and for statement E, it is taken as 201.
  4. If you introduce any error in statement B then the compiler will display the error at #line number 100.
  5. C has provided two special identifiers: __FILE__ and __LINE__, which indicate the file name of the source file and the current line number, respectively.
Point to Remember
#line is used to indicate line numbers which can be used for debugging.
MACRO
Introduction
Macros allow replacement of the identifier by using a statement or expression. The replacement text is called the macro body. C uses macros extensively in its standard header files, such as in getchar(), getc().
Program
#define CUBE(x)  x*x*x      //A
#include <stdio.h>
main ()
{
    int k = 5;
    int j = 0;
    j = CUBE(k);            //B  j = k*k*k

    printf ("value of j is %d\n", j);      //C
}
Explanation
  1. You can define the macro CUBE as in statement A.
  2. The macro can be defined by using parameters, but that is not mandatory.
  3. The parameter name that is used in a macro definition is called the formal parameter. In this example, it is x.
  4. x*x*x is called the macro body.
  5. There should not be any spaces between the macro name and the left parenthesis.
  6. CUBE(k) in statement B indicates a macro call.
  7. An argument such as k, which is used for calling a macro, is called an actual parameter.
  8. While expanding the macro, the actual parameter is substituted in the formal parameter and the macro is expanded. So you will get the expansion as j = k*k*k.
  9. The value of j is calculated as 125.
  10. Since macro expansion is mainly a replacement, you can use any data type for the actual parameter. So, the above macro works well for the float data type.
Points to Remember
  1. A macro is used when you want to replace a symbol with an expression or a statement.
  2. You can define macros by using parameters.
MACRO AND FUNCTION
Introduction
While writing the macro, you have to write the macro body carefully because the macro just indicates replacement, not the function call.
Program
#include <stdio.h>
#define add(x1, y1)  x1+y1   //E
#define mult(x1,y2) x2*y2   //F
main ()
{
    int a,b,c,d,e;
    a = 2;
    b = 3;
    c = 4;
    d = 5;
    e = mult(add(a, b), add(c, d)); //A

    // mult(a+b, c+d)               //B
    // a+b * c+d                    //C

    printf ("The value of e is %d\n", e);
}
Explanation
  1. Statement E indicates a macro for adding two numbers.
  2. Statement F indicates a macro for multiplying two numbers.
  3. Statement A indicates a macro that is supposed to add two numbers and then multiply two numbers. In this case, it is supposed to perform the calculation (2+3) * (4+5).
  4. The actual expansion of macro adds is given in statement B.
  5. The final expansion of mult gives the expansion a+b * c+d, which is erroneous.
  6. The final value of e is 17, which is not correct.
  7. To get the correct value, use the following definition:
8.  #define add(x1, y1) (x1+y1)
9.  #define mult(x2, y2) (x2*y2)
Point to Remember
While using the macro, you have to write the expression correctly. You can use parentheses to give the correct meaning to the expression