+ 3
Task about #define x 5+2 in C++ battle
There is following task in C++ battle. We have the code: #define x 5+2 void main() { cout x*x*x; } And there is question: what output will be? I thought that x=7 and so output is 7^3=343. But right answer is 27. Please, explain why output is 27. https://code.sololearn.com/c31k0ji5URfW/?ref=app
5 odpowiedzi
+ 6
Multiplication has higher precedence than addition. So in your case:
5+2*5+2*5+2 evaluates to 27
thats the nasty nature of #define I would suggest using constexpr instead.
+ 12
Try adding the line:
cout << 5 + 2 * 5 + 2 * 5 + 2;
And see what result you get.
+ 12
Like Kuba H has said..
cout << 5 + 2 * 5 + 2 * 5 + 2;
Becomes
cout << 5 + 10 + 10 + 2;
+ 7
Keep in mind that #define does not evaluate the expression it just copy paste it.