Stupid question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

21st Feb 2018, 1:34 PM
‎ ‏‏‎Anonymous Guy
7 Answers
+ 16
5 + 2 * 5 + 2 = 17 5 + 2 * 5 + 2 * 5 + 2 = 27
21st Feb 2018, 1:42 PM
Vukan
Vukan - avatar
21st Feb 2018, 1:35 PM
‎ ‏‏‎Anonymous Guy
+ 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)
21st Feb 2018, 2:27 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar
+ 5
Maybe,the trick is to add the multiplication first ?
21st Feb 2018, 4:12 PM
Alvian Muhammad
Alvian Muhammad - avatar
+ 2
thanks @vulcan
21st Feb 2018, 1:46 PM
‎ ‏‏‎Anonymous Guy
+ 1
yeah i think so
22nd Feb 2018, 1:08 AM
‎ ‏‏‎Anonymous Guy
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.
19th Jun 2018, 5:00 PM
Ashish Kumar Sahoo
Ashish Kumar Sahoo - avatar