2 2 votes What is the output of the following $C$ code segment? # define product (a, b) a * b main() { int x=5, y=2; print f("%d", product{x+4, y-3}); } $10$ $-9$ $15$ Error. Programming in C + – komal07 1.9k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
3 3 votes because MACRO product expands like this -product(x+4,y-3) expands to x + 4 * y - 3 => 5 + 4 * 2 - 3 => 5 + 8 - 3 =>10 (ANS) lokesh kumar answered Apr 11, 2015 lokesh kumar comment Share Follow 0 reply Please log in or register to add a comment.
2 2 votes follow the operator precedence rules . you will get ans 10. Product(x+4,y-3) expands to x + 4 * y - 3 => 5 + (4 * 2) - 3 => (5 + 8) - 3 =10. Instead of macro if you had used normal function like this int product(int a, int b) { return a*b; } then the answer will be -9 GATE_2016 answered Apr 12, 2015 • edited Apr 12, 2015 by GATE_2016 GATE_2016 comment Share Follow See 1 comment 1 1 comment reply _xor_ commented Apr 13, 2015 reply Follow flag if we pass values to a function which are in form of expression then it will be evaluated while in case of macro's expression is directly passed ????? is that right??? 0 0 replyShare Please log in or register to add a comment.
0 0 votes In C macro expands without any Braces or Parenthesis so here macro(a,b)= like a*b put whatever is in a or b without any brackets or something like that here (x+4) and (y-3) are given so macro expansion X+4*Y-3 now by precedence and associativity at first * will be computed then + and- so 5+4*2-3=13-3=10 A is correct answer Aboveallplayer answered Feb 22, 2016 Aboveallplayer comment Share Follow 0 reply Please log in or register to add a comment.