in Programming in C edited by
5,913 views
8 votes
8 votes

Consider the following statements

#define hypotenuse (a, b) sqrt (a*a+b*b);

The macro call hypotenuse(a+2,b+3);

  1. Finds the hypotenuse of a triangle with sides $a+2$ and $b+3$
  2. Finds the square root of $(a+2)^2$ and $(b+3)^2$
  3. Is invalid
  4. Find the square root of $3 *a+4*b+5$
in Programming in C edited by
by
5.9k views

4 Comments

after macro expansion it will become sqrt(a+2*a+2+b+3*b+3)

we will check the priority order , we know that  the priority of * is more than + so we solve it like {a+(2*a)+2+b+(3*b)+3}

it will be equal to a+2a+2+b+3b+3= 3a+4b+5

Ans is D
1
1
it's irrelevant to the answer but can someone confirm, option 1 and 2 look the same for me.
0
0
hypotenuse(a,b)  = sqrt(a*a+b*b)

hypotenuse(a+2,b+3) = sqrt(  a+2*a+2+b+3*b+3 )

                                   =  sqrt(  a+2a+2+b+3b+3 )

                                   =   sqrt( 3a+4b+5 )
0
0

2 Answers

16 votes
16 votes
Best answer

#define hypotenuse (a, b) sqrt (a*a+b*b);

hypotenuse(a+2,b+3)

after macro expansion it will become sqrt(a+2*a+2+b+3*b+3)

Now after evaluating you will get D.

selected by

4 Comments

I don't know macro expansion. Please expand this solution so that i can understand. Please
0
0

@dattasai and @Taiyaba Khatoon

'Macro expansion' means replacing a macro call in a program, by the definition of that macro.

Here, the macro call hypotenuse(a+2,b+3), wherever it appears in the program, is replaced by sqrt(a+2*a+2+b+3*b+3)

* has higher precedence than +, so the expression evaluates to $a + 2a + 2 + b + 3b + 3 = 3a + 4b +5$, instead of $\left ( a+2 \right )^{2} + \left ( b+3 \right )^{2}$

1
1
Would ((a+2),(b+3)) have worked fine?
0
0
1 vote
1 vote
Answer: