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

Discussion

Consider the following statements

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

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

  • A.Finds the hypotenuse of a triangle with sides a+2 and b+3
  • B.Finds the square root of (a+2)² and (b+3)²
  • C.Is invalid
  • D.Find the square root of 3*a + 4*b + 5

Answer: D

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

The macro body has no parentheses round its parameters, so the arguments are pasted in as raw text:

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

Multiplication binds tighter than addition, so this is \(\sqrt{a + 2a + 2 + b + 3b + 3} = \sqrt{3a + 4b + 5}\) - nothing like a hypotenuse. Writing #define hypotenuse(a, b) sqrt((a)*(a) + (b)*(b)) is what makes the macro behave as intended.

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

Post your comment

Please log in or register to post a comment.