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

3 Answers

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)

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
edited by
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
Position:
Show:

Related questions

5 5 votes
2 answers 2 answers
19.9k
19.9k views
Mandeep Singh asked Oct 22, 2016
19,918 views
#include <stdio.h int main() { int y = 2; int z = y +(y = 10); printf("%d\n", z); }
2 2 votes
2 2 answers
1.3k
1.3k views
komal07 asked Jul 7, 2015
1,338 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 1 vote
1 answers 1 answer
4.2k
4.2k views
Archies09 asked Apr 23, 2017
4,151 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...
1 1 vote
1 answers 1 answer
1.4k
1.4k views
abhinowKatore asked Jan 18, 2023
1,422 views
What will be the output of the following program ?answer is 8 but I am getting 5, please explain where I am wrong