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
- The statement H indicates
the #line directive.
- The #line number in statement B is
taken as 100 and for statement C, it is taken as 101.
- The #line number in statement D is
taken as 200 and for statement E, it is taken as 201.
- If you introduce any error
in statement B then the compiler will display the error at #line number 100.
- 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.
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
- You can define the macro
CUBE as in statement A.
- The macro can be defined by
using parameters, but that is not mandatory.
- The parameter name that is
used in a macro definition is called the formal parameter. In this
example, it is x.
- x*x*x is called the macro body.
- There should not be any
spaces between the macro name and the left parenthesis.
- CUBE(k) in statement B
indicates a macro call.
- An argument such as k, which is used for calling
a macro, is called an actual parameter.
- 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.
- The value of j is calculated as 125.
- 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
- A macro is used when you
want to replace a symbol with an expression or a statement.
- You can define macros by
using parameters.
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
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
- Statement E indicates a
macro for adding two numbers.
- Statement F indicates a
macro for multiplying two numbers.
- 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).
- The actual expansion of
macro adds is given in statement B.
- The final expansion of mult gives the expansion a+b * c+d, which is erroneous.
- The final value of e is 17, which is not
correct.
- 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