Why variable y is 0, is not 2? [duplicate] - c

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 5 years ago.
int x=0, y=0, z=0;
z = (x==1) && (y=2);
printf("%d ", y);
I'm wondering the output is 0.
Why the output is not 2?

Because of how C deals with the logical operators:
Since x==1 returns "false", there is no need to check the RHS in order to conclude that the end result of the operator && is false. Hence, RHS is not evaluated and y stays at its previous value.

Related

C: What does if(a<b<c) do? [duplicate]

This question already has answers here:
Comparing a variable to a range of values
(7 answers)
Closed 4 years ago.
why does the following if-statement return 1 (true)?
int main() {
short a = 1;
short b = 5;
short c = 4;
if (a<b<c)
printf("true \n");
else
printf("false \n");
return 0;
}
It is obviously not the same as
if(a<b && b<c)
because this returns false.
Thank you
The relational operators (<, <=, >, >=) are read from left to right (and have the same precedence) as you can see here: Operator precedence. Therefore
a < b
is evaluated first. The result of this evaluation (true or false then) will take part in the next evaluation
(1 or 0) < c
Essentially your code is the same as
if ((a<b)<c)
The < operator has left-to-right associativity. So your expression is parsed as follows:
(a<b)<c
So a<b is first evaluated. Since a is less that b it evaluates to true, i.e. 1. So now you have:
1<c
Since c is 4 this is also true, so the final result is 1.
The a<b statement is equal to true or 1. so we can say a<b or 1 is less than c.
printf(a<b); // result is 1
printf(1 < c) // result is true because 1 is less than 4
So this statement (a<b<c) is true
try online

Why post-decrements operator in the following C code is not working as desired? (to have the value of 7) [duplicate]

This question already has answers here:
Shortcircuiting of AND in case of increment / decrement operator
(6 answers)
Short circuit evaluation with both && || operator
(2 answers)
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
Why post-decrements operator in the following C code is not working as desired? (to have the value of 7). Knowing that it is not an undefined behaviour.
#include<stdio.h>
int main()
{
int a = 8, r;
r = (a==8) || (a--);
printf("a = %d\n", a);
return 0;
}
In the expression (a==8) || (a--); since (a==8) is already true hence rest of the OR condition is not evaluated.

Increment comparison truncate [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
Why is this expression:
int value = 0
if(++value == ++value) true?
Shouldn't that be equal to if(1 == 2) ? What is it equal to?
In c the value of the expression ++value == ++value is undefined. Technically, this is due to == not being a sequencing point..
Informally, this means that you don't know the order that evaluation of ++ and == will occur.

Why the second if condition is evaluated, even though the condition is not even satisfied? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
int main()
{
int a=10;
if (a == a--)
printf("true1\t\n");
a=10;
if(a == --a)
{
printf("true2");
}
return 0;}
as in the second if condition a=10 and --a is 9 i.e 10 != 9 so how come the second condition is evaluated ?
The value of --a is the previous value of a minus 1.
Furthermore that expression has the side-effect of changing the value of a.
The left part of the comparison is the value of a ... but is it the value of a before or after the side-effect has been applied?
The C Standard does not force the sequence of checking the value and applying the side-effect; and says that reading the value of a variable and changing its value without an intervening sequence point is Undefined Behaviour.
Basically there is a sequence point at every ; in the program (it's not as straightforward); your expression (a == --a) does not have sequence points.
I believe the next rule is also valid in C as in Java :
The --x has a higher priority than x--
a=10;
printf(a--) // should be showing 10;
a=10;
printf(--a) //should be showing 9

Mixed increment operators with logical operators [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 5 years ago.
I have a question concerning pre and post increments with logical operators
if I have this code
void main()
{int i = - 3 , j = 2 , k = 0 , m ;
m=++i||++j&&++k;
printf("%d %d %d %d",i,j,k,m);}
knowing that the increment and the decrement operators have higher precedence than && and ||
So they'll be executed first Then the && is higher than
means -2||3&&1 which gives the values -2 3 1 1 for the printf
but the output I get when trying on VS2010 is -2 2 0 1
Does anyone have any explanation for that ?
Regards,,
This is what you get from short circuiting. ++i is -2, and the rest doesn't have to be evaluated (and isn't according to the standard). The left side of || is true because -2 is not 0, so the whole expression is true.

Resources