+ 4
why the output of this code equal 1 ?
int x=10,y=20,z=5,i; i=x<y<z; printf("%d",i);
4 Respostas
+ 5
x<y<z
Think it as (x<y)<z
x<y is a condition which always leads to either true or false.
True means 1
False means 0
So, (x<y) => 10<20 => true => 1
Now, (x<y)<z => 1<5 => true =>1
So, i=1
+ 5
it will show a warning
the answer is for only x<y after that it will give you a warning
when we compare elements
the answer is given as true or false
in programing true = 1
and false = 0
here x < y is true ( so it is true = 1 )
it means x < y becomes 1
then 1 < z which is also true = 1 ( it will throw warning )
so i becomes 1;
hence final answer is 1.
/****************/
if you compare only x<y it will give you no warning but
compairing x<y<z will throw warnings
+ 2
I can see HOW i is equal to 1
Your assignment expression results as true for 10 is less than 20.
The WHY is a bit beyond my pay grade.... Perhaps someone else will have an explanation on why this behaves like that.
If you just want to assign i with the smallest value of your other 3 variables, I think a repeated ternary might be the way to do it. Again, this isn't something I'm 100% on the syntax, but maybe;
i = x ? x<y : y ? y<z : z;
+ 2
Ok thnx guys i understod it