0
How is this code's answer 10?
#define prod(i,j)i*j int main(){ int x=3,y=4; printf("%d",prod( x+2, y-1)); return 0; }
2 Réponses
+ 2
I think you copied the code wrong. Instead of
printf( "%d", ( x + 2, y - 1 ) );
I would guess the original line was
printf( "%d", prod( x + 2, y - 1 ) );
otherwise the result would be three, not ten.
Anyway, as to how the answer is ten, the explanation is that #define is a literal text replacement. Whenever the macro is encountered during precompilation, it is replaced by exactly the macro content.
Therefore, the print statement becomes
printf( "%d", x + 2 * y - 1 );
and from there, it is simply operator precedence that needs to be taken into account, i.e. multiplication before addition.
On a sidenote, please don't tag languages that are irrelevant to your question.
0
Thank you❤
I edited it..