+ 2

Why is the output generated here - 1

https://code.sololearn.com/czUCgLI29jTY/?ref=app

6th Oct 2020, 8:03 AM
Suhani Goyal
Suhani Goyal - avatar
7 Respuestas
+ 5
When you pass 4-1 The program takes it as this: 4-1 * 4-1 The above expression results in -1, because of bodmas. 4-(1*4)-1 4- 4 -1 in bodmas multiplication is done first compared to subtraction. Hope u get the idea, Suhani Goyal
6th Oct 2020, 8:30 AM
Steve Sajeev
Steve Sajeev - avatar
+ 4
Your code will solve look like this In macro u defined #define square(x) x*x printf("%d",square(4-1)); square(4-1) you defined x*x So 4-1*4-1 Here multiplication have highest precedence so 4-4-1=-1 but if u will write like this #define square(x) (x)*(x) noticed here i added bracket so bracket have highest precedence So it will calculate like this (4-1)*(4-1) So in this case (3)*(3)=9 output will be 9 .............. Thankyou .............
6th Oct 2020, 9:03 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Aditya rout she has doubt's that's why she asked . So don't post unnecessary comments in others post.
6th Oct 2020, 11:09 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Thanks for pointing it out. ♨️♨️ I didnt remember this.
6th Oct 2020, 9:20 AM
Steve Sajeev
Steve Sajeev - avatar
+ 2
You should use parenthesis to give priority to the expression like this square((4-1)). What will it do for you, it will simply give the value 3*3 instead of 4-1 *4-1 which is -1. If you want to discuss at a very high level checkout my profile
7th Oct 2020, 4:57 AM
Niteesh
+ 1
But square of 3 is 9, why is the answer - 1
6th Oct 2020, 8:28 AM
Suhani Goyal
Suhani Goyal - avatar
- 1
#include <stdio.h> #define square(x) x*x int main() { printf("%d",square((4-1));//brackets return 0; } you forgot the math rules?
6th Oct 2020, 11:06 AM
Aditya rout
Aditya rout - avatar