The Gateway to Computer Science Excellence
+6 votes
2.5k views

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 by Active (3k points) | 2.5k views
0
Is the answer D.
0
yes, how?
+3

#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.

0
Can u post it as an answer..?
+1

from the book let us c:

Be careful not to leave a blank between the macro template
and its argument while defining the macro. For example, there
should be no blank between AREA and (x) in the definition,
#define AREA(x) ( 3.14 * x * x )
If we were to write AREA (x) instead of AREA(x), the (x)
would become a part of macro expansion, which we certainly
don’t want. What would happen is, the template would be
expanded to
( r1 ) ( 3.14 * r1 * r1 )
which won’t run. Not at all what we wanted.

0
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
0
it's irrelevant to the answer but can someone confirm, option 1 and 2 look the same for me.

2 Answers

+14 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.

by Active (1.7k points)
selected by
+1
Perfect.
0
But sqrt(a,b) has not defined in micro calls. so there is no answer for this
0

Sumit1311

can u please elaborate the evaluation of the macro expansion to get the option D

0
I don't know macro expansion. Please expand this solution so that i can understand. Please
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}$

0
Would ((a+2),(b+3)) have worked fine?
+1 vote
by Loyal (9.9k points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,324 answers
198,404 comments
105,169 users