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.
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:
Logical Operators and increment operators [duplicate]
(1 answer)
Logical Operators in C
(8 answers)
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
Here is the code
int main()
{
int i=-3, j=2, k=0, m;
m = ++i||++j&&++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
And output:
-2, 2, 0, 1
But i don't understand the line m = ++i||++j&&++k; How it get's executed.
Someone please explain..Thanks!
Initially you have the 4 variables:
i = -3
j = 2
k = 0
m is uninitialized
m = ++i||++j&&++k; is executed left to right. so first one is ++i - I suggest reading about the differences between i++ and ++i - In this case i is increased by 1 and become i=-2
-2 is a true expression, therefore m becomes 1 and the rest of the expression is not evaluated. Because true or anything else is always true anyway.
So final outcome:
i = -2 (increased)
j = 2 (unchanged)
k = 0 (unchanged)
m is 1 (true)
Logical OR operation (expr1 || expr2) employs short-circuiting behavior. That is, expr2 is not evaluated if expr1 is logical 1 (true).
The expression with logical OR operator evaluate to true if any of the two operands is non-zero.
In this expression:
m = ++i||++j&&++k;
|_| |______|
LHS RHS
The i is initialized with -3. ++i will evaluate to -2.
-2 is a non zero value hence evaluate to logical true and the RHS part of the expression will not evaluated.
Since the whole expression evaluated to true, the value 1 is assigned to m.
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:
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:
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.