0
[SOLVED] Please help! Why isn't the answer 0 here?
int x =10, y = 20, z = 5, i; i = x<y<z; printf("%d\n", i);
8 Réponses
+ 5
It would be correct to write the expression like this:
i = (x < y) < z;
Agree, now it immediately became clear that x < y is true, that is, 1
and 1 < z is also 1,
so i = 1.
P. S: "You just need to remember that the compiler reads the code from left to right, from top to bottom, and the parentheses are here so that the compiler does not issue warnings because of the ambiguity of this expression."
+ 1
You're missing the main function and the standard input ouput header take a look
https://code.sololearn.com/cAk3zuLzWwq7/?ref=app
+ 1
Slick , thanks for reply. I thought it was 0 but the answer was 1. I just asked that why isn't 0?
+ 1
Vasiliy, thanks for reply. Actually I don't understand how the braces are put there? How I can understand the braces property?
+ 1
The braces handle everything internally, so whatever is in the braces is calculated before preforming operations on anything else, once the operation is completed, the parenthesis "disapear" and whatever was computed is then calculated with everything else.
Think of PEMDAS
+ 1
Thanks for the explanation Slick
- 1
https://code.sololearn.com/cSK7KKZYJ2Yu
#include <stdio.h>
int main() {
int x=10,y=20,z=5,i;
// For getting output when 1 the given logic:
i=(x<y)<z;
printf("%d",i); return 0;
}
- 2
0