This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
Closed 9 months ago.
I would like to know how i is evaluated in this code in C language ?
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d\n",i);
The result of a relational operator is either integer 1 if the condition is true or 0 otherwise. And relational operators evaluates from left to right.
So this statement
i = x < y < z;
is equivalent to
i = ( x < y ) < z;
and as x is less than y then it can be also rewritten like
i = 1 < z;
that initialize the variable i by 1 because 1 is less than 5.
From the C Standard (6.5.8 Relational operators)
6 Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false.107) The result has
type int.
If you will rewrite the statement like
i = x < y && y < z;
then the result of the expression will be equal to 0 because y is not less than z.
Related
given that
int w = 1;
int x = 6;
int y = 5;
int z = 0;
z = !z || !x && !y;
printf("%d\n", z);
z = x-- == y + 1;
printf("%d\n", z);
Could someone explain how the line below would evaluate to 1 if x-- is 5 and y+1 is 6?
z = x-- == y + 1;
The expression x-- evaluated to the value of x before being decremented.
So x-- == y + 1 is the same as 6 == 5 + 1 which is true, then the value 1 is assigned to z.
The expression
z = x-- == y + 1
will be parsed as
z = ((x--) == (y + 1))
IOW, you are comparing the result of x-- to the result of y + 1, and assigning the result of that comparison to z.
The result of x-- is the current value of x; the side effect is that x is decremented. So, given
x == 6
y == 5
then
x-- == 6
y + 1 == 6
(x-- == y + 1) == 1
so 1 is assigned to z; after this evaluation, x will equal 5.
Note that C does not force left-to-right evaluation in this case - y + 1 may be evaluated before x--. Also be aware that the side effect of the -- operator on x does not have to be applied immediately after evaluation - the whole thing could be evaluated as
t1 <- y + 1
t2 <- x
z <- t2 == t1
x <- x - 1
or
t1 <- x
t2 <- y + 1
x <- x - 1
z <- t1 == t2
or any other order. The update to x and the assignment to z can happen in any order, even simultaneously (either interleaved or in parallel).
While idiomatic C is often a little head-scratchy, this takes things a bit too far. As an academic exercise for learning about operator precedence and side effects it's okay (not great), but anyone who did this in production code could expect some grief over it in a code review. If nothing else it doesn't scan well - just adding some parens (like I did above) would greatly help with readability.
From the C Standard (6.5.2.4 Postfix increment and decrement operators)
2 The result of the postfix ++ operator is the value of the operand.
As a side effect, the value of the operand object is incremented (that
is, the value 1 of the appropriate type is added to it).
3 The postfix -- operator is analogous to the postfix ++ operator,
except that the value of the operand is decremented (that is, the
value 1 of the appropriate type is subtracted from it)
Thus in this expression statement
z = x-- == y + 1;
that may be equivalently rewritten like
z = ( x-- ) == ( y + 1 );
the value of the postfix decrement expression x-- is the value of the variable x before decrementing. That is the value of the expression is equal to 6.
The value of the expression y + 1 is also equal to 6 because the value of y is equal to 5.
And at last (the C Standard, 6.5.9 Equality operators)
3 The == (equal to) and != (not equal to) operators are analogous to
the relational operators except for their lower precedence.108) Each
of the operators yields 1 if the specified relation is true and 0 if
it is false. The result has type int. For any pair of operands,
exactly one of the relations is true
So as the value of the expression x-- is equal to the value of the expression y + 1 then the variable z gets the value 1.
You could get the expected by you result keeping the postfix decrement operator the following way
z = ( x--, x == y + 1 );
by inserting the comma operator. In this case after the comma operator there is a sequence point that means that in second operand of the comma operator x == y + 1 x will be already equal to 5.
On the other hand, if you will write for example the expression like this
z = --x == y + 1;
where instead of the postfix decrement operator there is used unary decrement operator -- then the value of the expression --x will be equal to 5 (the value of the unary decrement operator is the value of its operand after decrementing). In this case the variable z gets the value 0 because 5 is not equal to 6 (the value of the expression y + 1).
This was a question in my preparation exam:
int val = 0;
int x = 0;
int y = 1;
if (x < val < y)
printf(" true ");
else
printf(" false ");
Why is this true? I tried changing x and val and it ignored those changes, as long as y was larger than 0 (so 1, 2, 3...) the statement was true. So for example: if (3 < 9 < 2) will be true.
( 2 < 9 < 3 ) is evaluated as ( ( 2 < 9 ) < 3).
In the first step 2 < 9 is evaluated to be true, which is represented as integer value 1 and results in ((1) < 3) for the second step.
That is obviously true.
You probably wanted something like ((x < val) && ( val < y)).
The first is check whether x < value. If true, it return 1 so the next is check whether 1 < y.
First you check x and val. If its value is correct, you convert it to 1 and check it with y.
If you build with -Wall option using GCC compiler you will get a warning like this:
<source>:7:11: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]
if (x < val < y)
Detailed explanation of this warning can be found in GCC documentation. The relevant part is as following:
-Wparentheses
...
Also warn if a comparison like x<=y<=z appears; this is equivalent to (x<=y ? 1 : 0) <= z, which is a different interpretation from that of ordinary mathematical notation.
...
NOTE: Comparisons in C follow the left to the right approach. (if all operators are of same priority)
Thus, evaluation of (2 < 9 < 3) will be done in 2 steps:
step 1. evaluation of (2 < 9): it is true, thus, integer value=1.
step 2. evaluation of ( (2 < 9) < 3 ) i.e (1 < 3): it is also
true.
I think you wanted (2 < 9) && (9 < 3).
In C, usually operators follow left to the right approch(except operators like =, -=, +=, etc.). Operator <, > follows left to the right approch too. So, 2<9<3 in C means (2 < 9) < 3. 2 < 9 is 1, so, the value of 2 < 9 < 3 is 1.
You can fix it with (2 < 9) && (9 < 3).
int x = /* some integer */;
unsigned int ux = (unsigned) x;
we have
x >= 0 || x < ux
we know that in x < ux the first x is cast implicitly to unsigned but is the first x in x >= 0 (1) cast to unsigned implicitly?
No. It happens operator by operator.
x >= 0 || x < ux
is naturally
(x >= 0) || (x < ux)
Since x and 0 are both ints, there is no need for any (usual arithmetic) conversions...
And even though x is converted to unsigned in x < ux, the value of the expression x < ux is of type int - either 0 or 1 (just like on the the left-hand side).
No it isn’t.
This is because x >= 0 is an expression. (Formally 0 is an octal constant of type int.)
Try 1 / 2 * 1.0 for a more pernicious example. This is grouped as (1 / 2) * 1.0 and is zero since the integers in the expression 1 / 2 are not promoted to floating point.
No, 0 is an int, so there are no promotions in the x >= 0 part of your expression.
#include<stdio.h>
main()
{
int x = 5, y = 10, z = 10;
x = y == z; // This computational expression causes the value of x to be 1. I fail to understand why
printf("%d\n", x); //Why is the value of x 1 here.
}
I fail to understand the statement x = y ==z;
According to me - x = 10 since y == z. z=10 and is stated to be equivalent to y. The value of y is then assigned to x - x = y
You assign the result of the comparison »Is y equal to z« to x, which is 1, i.e. true.
Note the different operators:
x = ... // assignment
y == z // comparison with either 0 (false) or 1 (true) result
Let's break the program down a little bit further:
You initialize x to 5 and y and z to 10.
You perform a comparison (see above) of y and z without caring for the result. So that's a line that can safely be ignored. But it results in 1 since y is equal to z.
You print the current values of all three variables.
You perform the same comparison, this time assigning the result to x. x now has the value »Is y equal to z«, which is 1 in this case.
Because of operators precedence
x = y == z;
is the same as
x = (y == z);
Now as y == z evaluates to 1, so x value is 1 after the statement.
y == z returns 1 if they are the same and 0 if they are not and the result of this is set to x
== is a comparison operator, so will return 1 (true) if both of the operands are equal and 0 (false) if they are not.
The statement x = y == z is equivalent to x = (y == z), because == has a higher precedence than =. Because y is equal to z, this will assign 1 to x.
Please refer to Operator Precedence Table. == (Comparison Operator) has a higher precedence over = (Assignment Operator). So, the y == z gets executed first and then yields result of 1 as y and z are having the same values which results in x being assigned a value of 1.
= is the assignment operator and == is the comparison operator. you compare y and z, they are equal, so the comparison returns true which is 1. this value is assigned to x.
the == operator acts first.
Hence it becomes x= (value of y==z);
now since y and z are the same, the value of y==z is 1 that gets assigned to x.
== has a greater precedence than =.
The expression y == z is evaluated to 1.
The instruction x = y == z puts 1 in x.
The instruction printf("%d\n", x); prints the value of x (1).
The precedence of == operator is higher than = operator.
y==z evaluates to 1; since both are equal.
Thisn value gets assigned to x.
For the following code snippet I get the output as 1. I want to know how it came?
void main()
{
int x=10,y=20,z=5,i;
i=x<y<z;
printf("%d",i);
}
i=x<y<z;, gets interpreted as i=(x<y)<z, which in turn gets interpreted as i=1<z, which evaluates to 1.
10 is less than 20, resulting in 1, and 1 is less than 5, resulting in 1. C doesn't chain relational operators as some other languages do.
It operates as follows:
Since < is a logical expression, x<y i.e 10<20 is true i.e 1. So it becomes 1<z i.e 1<5 which is again true i.e. 1 which is assigned to i. So i is 1.
This is because your code evaluates as:
void main()
{
int x=10,y=20,z=5,i;
i=((x<y)<z); //(x<y) = true = 1, (1 < 5) = true
printf("%d",i);
}
what output did you want?
In C,
i = 2 < 3; //i == 1.
i = 4 < 3; //i == 0.
If condition evaluates to false, value returned is 0, and 1 otherwise.
Also, x < y < z will be evaluated as ((x < y) < z).
x<y // 1 as (10 < 20) will return 1
result of(x<y)<z // 1 as (1<5) will return 1
C++ doesn't support multi-part comparisons like that.
x < y < z
is interpreted as
(x < y) < z
or that is, determine if x < y, then see if that boolean is less than z.
There's some discussion on why that is over at the software engineering StackExchange.
When you find yourself trying to do this, instead you need to write it as two separate comparisons joined by a boolean:
(x < y) && (y < z)