Output question: c++


#1

#include<stdio.h>
#define square(x) x*x
int main(){
int i;
i = 8/square(4);
printf("%d ",i );
}


#2

square(4) will return 4 * 4 then
i = 8/ 4 * 4 = 2* 4 = 8.

Practice other questions on define too,as they used to always give a different answer to what we think.


#3

In this code the compiler will do the mapping means the x*x will be placed at the position of square(x) in the main section
ie
i=8/4 * 4
So the answer will be 8/4=2 * 4 =8 answer.

If in the code would have written like this then
#define square(x) (x*x)
The output would be 0


#4

The following code will evaluate the expression as follows:
i=8/44, (Macro square returns xx hence 4*4).
Precedence of / and * operators are the same so execution occurs from left to right.
Since there is no brackets to define order of evaluation, execution takes place as follows
i=(8/4)2
i=4
2
i=8
Final Ans i=8