0
Explain tha code and find the output?
#include <stdio.h> #define fun(x,y)(x>y)?(x*y):(y-x); int main() { int p=7,q=6,r=4; r+=fun(++p,q++); printf("%d%d%d",p,q,r); return 0; }
1 Answer
+ 3
Since the macro is just a text replacement, the line
r += fun( ++p, q++ );
becomes
r += ( ++p > q++ ) ? ( ++p * q++ ) : ( q++ - ++p );
after the preprocessor is finished.
Substituting the values shows that
8 > 6
is true, hence the first part of the ternary operator is executed, which evaluates to 9 * 7 = 63.
Therefore, at the end you get p = 9, q = 8 and r = 67.
If you want to confirm the output, just paste the code into the playground and try it yourself.