Interview Question


#define Sqr(b) b*b;
main( )
{
int i=3;
printf("%d",Sqr(i+2));
}

output = ?

The ans is 11. This is because while using #define to declare functions, the statement is taken as it is and not computed. So if Sqr(i+2) is called, the resulting statement would be i + 2 x i + 2. Now this is computed in left to right order in order of decreasing preference. So this would give 3 + 2 x 3 + 2 (as x is higher than +) which equals 3+6+2 = 11.

2 Likes