0
How the operator works here?
#include <stdio.h> int main() { int x=0,y=1,z; z=++x?x<y||x>y?1:11:111; printf ("%d %d %d",x,y,z); return 0; }
1 Odpowiedź
+ 4
First you declared x=0 and y=1,
Then ,
z=++x?x<y||x>y?1:11:111;
This line has used a ternary operator. If you don't know how does it work you can have a look at it:
https://www.sitesbay.com/cprogramming/c-ternary-operator#
The line is same as:
if(++x)
{
if(x<y||x>y)
{
z=1;
}
else
{
z=11;
}
}
else
{
z=111;
}
++x makes the value of x to 1 and then checks if the value of x!=0.
So the condition becomes true since it's 1 !=0.
Then it goes to the first condition, x<y||x>y.
Since x==y so the condition doesn't meet. So it goes to the else statement and the value of z becomes 11.
Now it prints the value of x,y, and z which is 1,1, and 11.