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
- 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:
#define add(x1, y1) (x1+y1) #define mult(x2, y2) (x2*y2)
No comments:
Post a Comment