Please explain the output — I am getting the output as 20, but it should be 64 if I am not wrong.
#include<stdio.h>
#define SQUARE(X) (X*X)
main()
{
int a, b=6;
a = SQUARE(b+2);
printf("\n%d", a);
}
Please explain the output — I am getting the output as 20, but it should be 64 if I am not wrong.
#include<stdio.h>
#define SQUARE(X) (X*X)
main()
{
int a, b=6;
a = SQUARE(b+2);
printf("\n%d", a);
}
The correct result is 20.
Macros are simple text substitutions.
To see that the result is 20, just replace the X
with b+2
. Then you have:
b+2*b+2
as b
is 6 it is
6+2*6+2
which is 20
When using macros it is important to use parenthesis so the macro should look
# define SQUARE(X) ((X)*(X))
Then the result will be 64 as the evaluation is
(b+2)*(b+2)
(6+2)*(6+2)
8*8
64