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)2 and (b+3)2
(C) Is invalid
(D) Find the square root of 3*a + 4*b + 5
Answer: (D)
Explanation:
A macro is defined with, hypotenuse (a, b) sqrt (a*a+b*b); call hypotenuse(a+2,b+3); hypotenuse = sqrt (a+2*a+2 + b+3*b+3) = sqrt (a + 2a + 2 + b + 3b + 3) = sqrt (3a + 4b + 5)
So, option (D) is correct.
Note: correct way to define this macro is:
#include <stdio.h> #include <math.h> //space is not allowed before bracket #define hypotenuse(a, b) sqrt(a*a+b*b) int main()
{ //assume a = 1, b = 2
printf ( "%f" , hypotenuse (1+2, 2+3));
//output should be 4.000000
return 0;
} |
Article Tags :
Recommended Articles