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; }

21st Apr 2020, 11:34 AM
Ashfaq Reshad
Ashfaq Reshad - avatar
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.
21st Apr 2020, 11:58 AM
Shadow
Shadow - avatar
0
Thank you❤ I edited it..
21st Apr 2020, 12:01 PM
Ashfaq Reshad
Ashfaq Reshad - avatar