Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following statement
#define hypotenuse (a, b) sqrt (a*a + b*b);
The macro-call hypotenuse (a + 2, b + 3);

  • A.finds hypotenuse of triangle with sides a + 2 and b+3
  • B.finds square root of (a + 2)2 + (b + 3)2
  • C.is meaningless
  • D.finds square root of 3*a + 4*b + 5

Answer: D

Correct answer: finds square root of 3*a + 4*b + 5

The macro's parameters are not parenthesised in the replacement text, so the arguments are substituted textually and the surrounding operators then bind more tightly than intended:

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

which is \( \sqrt{a + 2a + 2 + b + 3b + 3} = \sqrt{3a + 4b + 5} \). The intended \( (a+2)^2 + (b+3)^2 \) is not what results. This is the standard argument for parenthesising every parameter and the whole replacement text in a function-like macro.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.