+ 7

// Community Downvotes

//Declined due to community downvotes. Please review and resubmit What is the output of this code? #define sqr(x) (x*x) int main() { cout<<sqr(5+2); } Seriously ? Why this code has more downvotes when it doesn't have complicated maths. If you know the concept of #define this question isn't a problem at all. But why down voting it :/ People will like cout<<1+2; question but not like this type of questions.Solo Learn should not decline quiz based on likes :/ P.s: My English isn't good though!

7th Mar 2017, 6:53 AM
Mr.Robot
Mr.Robot - avatar
5 Réponses
+ 7
for starters, just checked and it doesn't work well i expected the result 49 and got 17 (5*2+5+2 perhaps???) o_O only when used with another set of brackets sqr((5+2)) did i get what i expected
7th Mar 2017, 7:05 AM
Burey
Burey - avatar
+ 7
@burey , In c++ , #define used to replace the parameter before the compiler executes. So in the program , #define sqr(x) (x*x) will replace the (x*x) in sqr(x) before compiler executes so sqr(5+2) = (5+2*5+2) = 17 Your answer 49 will come if the #define sqr(x) (x)*(x) is defined like this way. I hope you understood. :D
7th Mar 2017, 7:09 AM
Mr.Robot
Mr.Robot - avatar
+ 7
understood now thanks could be that many of those who downvoted expected the same answer as me and when didn't get it just treated the quiz as faulty
7th Mar 2017, 7:21 AM
Burey
Burey - avatar
+ 6
You can do this #include <iostream> using namespace std; #define sqr(x,y) ((x*x)+(2*x*y)+(y*y)) int main() { cout<<sqr(5,2); return 0; }
7th Mar 2017, 7:11 AM
Prabhakar Dev
Prabhakar Dev - avatar
+ 6
I think I didn't understand the question
7th Mar 2017, 7:14 AM
Prabhakar Dev
Prabhakar Dev - avatar