+ 6
Stupid question
#include <iostream> #define X 5+2 using namespace std; int main() { cout << X <<endl; cout <<X*X<<endl; int i =X*X*X; cout<< i; return 0; } output: 7 17 27 why isn't the output 7 49 343 Thanks
7 RĂ©ponses
+ 16
5 + 2 * 5 + 2 = 17
5 + 2 * 5 + 2 * 5 + 2 = 27
+ 5
link to the code
https://code.sololearn.com/cMNUXrpoGO0W/?ref=app
+ 5
Macros simply do text substitution. They will not care about the operator precedence. As long as 'X' is a single operand there is no problem. If you want to be safe, It is better to enclose #define expressions in brackets like the following
#define X (5+2)
+ 5
Maybe,the trick is to add the multiplication first ?
+ 2
thanks @vulcan
+ 1
yeah i think so
0
The answer which has given by Android above is correct.Here X is initialize to 5+2,so the value of X is 5+2 not 7. Then by putting this value you will get the answer correctly.