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);

20th Aug 2021, 8:41 PM
mesarthim
mesarthim - avatar
8 Answers
+ 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."
20th Aug 2021, 9:10 PM
Solo
Solo - avatar
+ 1
You're missing the main function and the standard input ouput header take a look https://code.sololearn.com/cAk3zuLzWwq7/?ref=app
20th Aug 2021, 8:57 PM
Slick
Slick - avatar
+ 1
Slick , thanks for reply. I thought it was 0 but the answer was 1. I just asked that why isn't 0?
20th Aug 2021, 9:10 PM
mesarthim
mesarthim - avatar
+ 1
Vasiliy, thanks for reply. Actually I don't understand how the braces are put there? How I can understand the braces property?
20th Aug 2021, 9:13 PM
mesarthim
mesarthim - avatar
+ 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
20th Aug 2021, 9:15 PM
Slick
Slick - avatar
+ 1
Thanks for the explanation Slick
20th Aug 2021, 9:18 PM
mesarthim
mesarthim - avatar
- 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; }
21st Aug 2021, 1:13 AM
Abhishek Kumar
Abhishek Kumar - avatar
- 2
0
26th Sep 2021, 9:29 AM
Ahmed AL sadain