Reputation: 120
why the result is x=1 y=3 res=1
int x = 7, y = 3;
int res;
res = (x = y < 2 || x != 1);
printf("x = %d y = %d res = %d\n", x, y, res);
and with this code the result is y<2 so False which is 0 so lvalue x = 0 so the res=0
res= (x = y < 2); //|| x != 1);
printf("x = %d y = %d res= %d\n", x, y, res);
Upvotes: 0
Views: 82
Reputation: 106216
res = (x = y < 2 || x != 1);
...is evaluated as...
res = (x = ((y < 2) || (x != 1)));
You can find the Operator Precendence for C++ here - C is similar for the operators you've used.
So for x = 7, y = 3
...
res = (x = ((3 < 2) || (7 != 1)));
res = (x = (false || true)); // || is "logical-OR", tests if either true
res = (x = true);
res = (x = 1); // standard conversion from bool to int
res = 1;
For your second, simpler statement:
res = (x = y < 2);
res = (x = (y < 2));
res = (x = (3 < 2));
res = (x = false);
res = (x = 0); // standard conversion from bool false to int 0
res = 0;
In C, even if you #include <stdbool.h>
the <
, !=
and ||
operators will yield 1
immediately for "true" tests, and 0
for "false", and there's no separate "standard conversion" as in C++. Allan's answer describes the C evaluation steps nicely.
Upvotes: 5
Reputation: 4041
In this case operator precedence will work.
res = (x = y < 2 || x != 1);
In this expression, first y < 2 is evaluated.
Then or(||) will work, because of first condition fails, so now the x is not equal to 1
. so the condition will became true so the true value 1
is stored x and in the res variable.
(y < 2 || x != 1 ). // expression is true. So the value 1 is stored in the variable x and ret.
In second case,
y < 2; // this condition will become false. So the zero is stored in both the variable.
Upvotes: 0
Reputation: 85
The issue you seem to be having is operator precedence. The link is to an operator precedence chart which should help clear things up.
First you should know that comparisons result in either a 1 or a 0. For example, (5 < 10) returns 1 because it is true, and (5 > 10) returns 0 because it is false.
In your example, it's easier if we clarify it by adding parenthesis to show the order things are happening in.
res = (x = **(y < 2)** || **(x != 1)**)
This is the first set of operations that happens. After those resolve we are left with:
res = (x = **(0)** || **(1)**)
The next operation to take place is the OR:
res = (x = **(0 || 1)** )
This results in a 1:
res = (x = **1** )
then the assignment operation happens:
res = (**x = 1**)
then the next assignment happens:
**res = x**
Since x is equal to 1 from the previous assignment, res is also set to 1.
Not sure what you're asking in the second bit, so if you want to clarify that I'll answer for you.
Upvotes: 2
Reputation: 2547
res = (x = y < 2 || x != 1);
In above statement y < 2 || x != 1
is a conditional expression whose result is true (1)
which is loaded into x
.
Now, (x = y < 2 || x != 1)
evaluated as x = 1
and hence res = x = 1
This you get res
and x
equals to 1
and y
unchanged.
Upvotes: 2
Reputation: 106092
<
has higher precedence than =
and therefore x = y < 2
is equivalent to x = (y < 2)
.
Since y
is greater than 2
therefore y < 2
will give 0
and this value will be assigned to x
. Now x
is equal to 0
therefore x != 1
will be evaluated as true
and the value of the whole expression x = y < 2 || x !=
will be 1
and this value is assigned to res
.
Upvotes: 1