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.
Related
This question already has answers here:
How || and && works [duplicate]
(5 answers)
Closed 3 years ago.
I am having trouble understanding why the following code:
0 || -1
Evaluates to 1? More specifically, I am confused as to what the || and && operators mean when applied to integers.
Every expression value != 0 evaluates to 1, if value is not equal to zero. (see comment from #MiCo and #M.M.)
|| is an or operation with two operands. If the left or the right operand is not zero the or operation evaluates to 1.
Since -1 is not 0 it evaluates to 1,
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.
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.
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
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.