I didn't get this program, why it returns 9 but not 25 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

I didn't get this program, why it returns 9 but not 25

#define square(x) x*x Int main() { printf("%d", square (4+1)); return 0; }

1st Feb 2021, 2:12 PM
Surkhab Khan
Surkhab Khan - avatar
13 Answers
+ 18
It is working like this : 4+1*4+1=4+4+1=9
1st Feb 2021, 2:15 PM
Abhay
Abhay - avatar
+ 11
If you want your output to be 25: #define square(x) (x)*(x) int main() { printf("%d", square (4+1)); return 0; } // this returns 25 because (x) = (4+1) = (5) and not the way Abhay has point out // (x)*(x) = (5)*(5) // parenthesis have higher priority than most of the operations.
1st Feb 2021, 2:20 PM
Rohit
+ 8
Surkhab Khan If there is brackets like this square(x) (x) * (x) then output will be 25
1st Feb 2021, 2:49 PM
A͢J
A͢J - avatar
+ 5
I Am AJ ! that's what i meant if there is parenthesis included then the o/p is 25 if no parenthesis is included like so x*x then it will be executed like you and Abhay said as follow 4 + 1 * 4 + 1 = 4 + 4 + 1 which is indeed 9.
1st Feb 2021, 2:55 PM
Rohit
+ 4
Sorry Surkhab I was wrong. It is like this square(x) = x * x square(4 + 1) = 4 + 1 * 4 + 1 = 4 + 4 + 1 = 9
1st Feb 2021, 2:44 PM
A͢J
A͢J - avatar
+ 4
RKK See his question again there is no brackets in macros so output will be 9 Surkhab Khan Understand here when it's 25 and when it's 9 https://code.sololearn.com/cR1y8UCa5HC1/?ref=app
1st Feb 2021, 2:50 PM
A͢J
A͢J - avatar
+ 2
Surkhab Khan its 25 not 9 check it #include <stdio.h> #define square(x) (x)*(x) int main() { printf("%d", square (4+1)); return 0; }
1st Feb 2021, 2:43 PM
Rohit
+ 2
I Am AJ ! Thanks sir ....... I got this
1st Feb 2021, 2:47 PM
Surkhab Khan
Surkhab Khan - avatar
+ 1
I Am AJ ! but machine returns o/p 9 ....how
1st Feb 2021, 2:36 PM
Surkhab Khan
Surkhab Khan - avatar
+ 1
Abhay thanks bro
1st Feb 2021, 2:48 PM
Surkhab Khan
Surkhab Khan - avatar
0
RKK machine returns 9 🤔🤔
1st Feb 2021, 2:37 PM
Surkhab Khan
Surkhab Khan - avatar
0
by precedence the multiplication goes first and then the addition put parentheses in the sum #define square(x) x*x Int main() { printf("%d", square ((4+1))); return 0; }
2nd Feb 2021, 11:56 PM
Ruben Enrique
Ruben Enrique - avatar
0
According to (B,O,D,M,A,S) rule , Answer would be, 4+1*4+1= 4+4+1= 9 If , you will use brackets (4+1)*(4+1) ,then your and would be 25 ..
6th Feb 2021, 6:50 PM
Shubham Yadav
Shubham Yadav - avatar