C++ Programming :: Functions - Discussion
11 / 14
#include <iostream>
using namespace std;
#define SQ(a) a*a
inline int square(int a)
{
return a*a;
}
int main () {
int i, j, k, l;
i = SQ(2 + 3);
j = square(2 + 3);
k = SQ(6 - 1);
l = square(6 - 1);
cout << i << " "<< j << " "<< k << " " << l;
return 0;
}
#include <iostream>
using namespace std;
#define SQ(a) a*a
inline int square(int a)
{
return a*a;
}
int main () {
int i, j, k, l;
i = SQ(2 + 3);
j = square(2 + 3);
k = SQ(6 - 1);
l = square(6 - 1);
cout << i << " "<< j << " "<< k << " " << l;
return 0;
}
A11 25 -1 25
B11 11 -1 -1
C25 25 25 25
DNone of the above
Show Explanation
So,
SQ(2 + 3) will be replaced with 2 + 3 * 2 + 3 and '*' had more priority in operator precedence. i becomes 2 + 6 + 3 = 11.
square(2 + 3) is a regular function call and j becomes 25.
SQ(6 - 1) will be replaced with 6 - 1 * 6 - 1 and '*' had more priority in operator precedence. k becomes 6 - 6 - 1 = -1.
square(6 - 1) is a regular function call and l becomes 25.
Asked In ::
We defined a macro SQ as SQ(a) a*a. So whenever we use SQ() in our code compiler replaces that with macro definition which is a*a.
So,
SQ(2 + 3) will be replaced with 2 + 3 * 2 + 3 and '*' had more priority in operator precedence. i becomes 2 + 6 + 3 = 11.
square(2 + 3) is a regular function call and j becomes 25.
SQ(6 - 1) will be replaced with 6 - 1 * 6 - 1 and '*' had more priority in operator precedence. k becomes 6 - 6 - 1 = -1.
square(6 - 1) is a regular function call and l becomes 25.
Read Full Answer
Report Error
Please Login First Click Here