0
What will be the output? And why?
#include<stdio.h> #define square(x) (x * x) int main() { int x, y=2; x= square(y+1); printf("%d\n",x); return 0; }
2 odpowiedzi
+ 2
The output will be 5
The line x = square(y + 1);
Gers replaced by "square" macro:
x = y + 1 * y + 1;
=> x = y + y + 1; => x = 2 + 2 + 1; ≠> x = 5
If yoi want square(x) gives the "right" answe, then you must add ( ) like this:
#define square(x) (x)*(x)
Now the line will be replaced by:
x = (y + 1) * (y + 1);
+ 1
Given that square (x) which considered as (y+1) and macro has (x*x)
Since we know x=y+1;
(X*X) means ( y+1*y+1 ) which is not equal to (y+1)*(y+1) ; so answer will be y+1*y+1=y+y+1;
Given:y=2;
Then 2y+1 gives [2*2+1=5]