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,
Related
This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
if(x==y==z) works, if(x!=y!=z) does not
(4 answers)
Closed 1 year ago.
Anyone can enlighten me on why -5<-2<-1 returns 0 in C when I would expect it to return 1(True)?
printf("%d", -5<-2<-1);
This expression
-5<-2<-1
is equivalent to
( -5<-2 ) < -1
because the operator < evaluates left to right.
As -5 is less than -2 then the value of the sub-exoression
( -5 < -2 )
is integer value 1. So you have
1 < -1
and the result of this expression is 0 that is logical false.
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. The result has type
int.
It seems you mean - 5 < -2 && -2 < -1
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.
This question already has answers here:
Evaluation of C expression
(8 answers)
Why isn't "k" incremented in the statement "m = ++i && ++j || ++k" when "++i&&++j" evaluates to true? [duplicate]
(5 answers)
Closed 8 years ago.
can some one draw the precedence tree for the expression and please explain the side effects..values after the expression evaluation in C.
int i=-3, j=2, k=0, m;
m= ++i || ++j&&++k;
according to me output should be -2 3 1 1 but my gnu c compiler printing is -2 2 0 1?
i want to know how?
Because j won't be evaluated due to short circuit evaluation:
m= ++i || ++j && ++k;
↑
At this stage, m is already evaluated to 1 regardless of the right side of the ||. Why?
Because 1 || anything is 1.
The && and || operators in C short-circuit. This means that if the value of their left hand side is enough to determine the overall value, the right hand side is never evaluated.
Your expression is parsed as (++i) || ((++j)&&(++k)). || short circuits, so after ++i has been evaluated, and its value has been found to be -2 (a true value), no more of the expression is evaluated.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Problem with operator precedence
we know that precedence of prefix is greater than "LOGICAL AND" (&&) and precedence of "LOGICAL AND" is greater than "LOGICAL OR" (||).
Below program seems to violate it:
int main()
{
int i=-3,j=2,k=0,m;
m=++i||++j&&++k;
printf("%d %d %d %d",i,j,k,m);
return 0;
}
If precedence of ++ is more than && and || then all prefix should execute first. After this i=-2,j=3,k=1 and then && will execute first.
why output shows : -2 2 0 1 ?
The behavior of the program is also same on ubuntu v12.04.
The && and || operators are "short-circuiting". That is, if the value on the left is FALSE for && or TRUE for || then the expression on the right is not executed (since it's not needed to determine the value of the overall expression).
It's correct because Short-circuiting definition.
m = ++i||++j&&++k
First, left part ++i is always TRUE so now i is -2 and it doesn't execute the right part of expression, the value of j,k don't change.