edited by
1,391 views
2 votes
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}); 
}
  1. $10$
  2. $-9$
  3. $15$
  4. Error.

edited by

3 Answers

3 votes
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)

2 votes
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
edited by
0 votes
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

Related questions

5 votes
5 votes
2 answers
1
Mandeep Singh asked Oct 22, 2016
18,375 views
#include <stdio.h int main() { int y = 2; int z = y +(y = 10); printf("%d\n", z); }
2 votes
2 votes
2 answers
2
komal07 asked Jul 7, 2015
702 views
main(){int a [3] ={ { {1,2},{9,8},{3,7} },{ {2,3},{1,4},{5,4} } };printf("%d %d %d", a -a[0],a [0]-a[0][0],a [0][0]-a[0][0][0]);}A) 3 3 1B) 3 6 1C) 6 6 1D) 1 1 1Plz expla...
1 votes
1 votes
1 answer
3
Archies09 asked Apr 23, 2017
3,321 views
What will be the output of the program?#include<stdio.h int addmult(int ii, int jj) { int kk, ll; kk = ii + jj; ll = ii * jj; return (kk, ll); } int main() { int i=3, j=4...
0 votes
0 votes
1 answer
4