+ 2
Why is the output generated here - 1
8 Réponses
+ 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
+ 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 .............
+ 4
Aditya rout she has doubt's that's why she asked . So don't post unnecessary comments in others post.
+ 2
Thanks for pointing it out. ♨️♨️
I didnt remember this.
+ 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
+ 1
But square of 3 is 9, why is the answer - 1
- 1
#include <stdio.h>
#define square(x) x*x
int main() {
printf("%d",square((4-1));//brackets
return 0;
}
you forgot the math rules?