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.
Related
This question already has answers here:
What is short-circuit evaluation in C?
(3 answers)
Closed 1 year ago.
I'm struggling to understand the behavior of the below code:
#include <stdio.h>
int main(void)
{
int i;
int j;
int k;
i = 7;
j = 8;
k = 9;
printf("%d\n", (i = j) || (j = k));
printf("%d, %d, %d\n", i, j, k);
return (0);
}
Output:
1
8, 8, 9
Question:
I understand that expr1 | | expr2 has the value 1 if either expr1 or expr2(or both)has a nonzero value.
The value of i increased from 7 to 8 because j's value is assigned to i but the same way why does the value of the j is not increased even though j = k? I was expecting an
output
1
8, 9, 9
From the C standard (emphasis mine):
Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the
second operand is evaluated, there is a sequence point between the evaluations of the first
and second operands. If the first operand compares unequal to 0, the second operand is
not evaluated.
The above behaviour is commonly referred to as operator short circuiting.
In your example, since (i = j) is not zero the second operand is thus not evaluated.
First of all,
true || true is true
true || false is true
false || true is true
false || false is false
So operator || is calculating the first expression and if it is true, it does not check the second one.
In yours particular situation (i = j) is equal to 8, which is considered true, because int values are considered false only if they are equal 0.
So the second (j = k) is never computed, which leads to your result.
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
This question already has answers here:
Short circuit evaluation and side effects [duplicate]
(3 answers)
Closed 6 years ago.
This is the exemplar code that I encountered in entry level Computer Programming course:
#include <stdio.h>
int main()
{
int l = 20, m = 10;
int z;
z= l++ || m++;
printf("z = %d l = %d m = %d\n", z, l, m);
}
The code prints l=21, m=10 and z=1
values of l and z are what was expected by me, but value of m is troubling me. Shouldn't it be 11 as m++ is present in code.
It is because m++ would only be executed if l++ == 0. Since l++ evaluates to 20 then m++ is never executed. If l was 0 initially then m++ would be executed.
z= l++ || m++;
In your expression, l first assign value and after incremented one.
In Logical or (||) operation, If the left operand is non-zero, then right operand is not evaluated and the result is true. That's why l become 21 and m not evaluated and it's value is 10.
C standard(N1256 : 6.5.14-paragraph 4) say's:
Unlike the bitwise | operator, the || operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand. If the first operand compares unequal
to 0, the second operand is not evaluated.
What you see is called short circuit evaluation.
Your line here:
z= l++ || m++;
Says, Check the value of l (increment while checking it). If l is non-0, set z to 1. If l is 0, check the value of m (increment while checking it). if m is non-0, set z to 1. Otherwise set z to 0.
Essentially, when the first of the two checks (l++) already evaluates true, there is no need for the system to check the second condition, so it doesn't, and thus also fails to increment m.
This question already has answers here:
Short circuit behavior of logical expressions in C in this example
(1 answer)
Operator Precedence vs Order of Evaluation
(6 answers)
Closed 7 years ago.
In the program shown below, prefix should be evaluated first because it has higher precedence, But answer is -2, 2, 0, 1 and it is explained in book "as LHS of || is true RHS is not evaluated."
Why is it so? All the increments should performed first and then logical should be checked because of precedence.
#include<stdio.h>
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;
}
Don't get confused with Precedence and Order of evaluation.
The order of evaluation of logical OR || is left to right.
So if left = true then left || right will never execute right. In your code exactly same happened.
As you know, any non zero value treated as true in C, hence, ++i or -2 is true. So,
m = ++i || ++j && ++k;
m = true || bla bla bla; //right not even checked!
m = true
m = 1
And you get the output as expected.
The logical && and || operators fully evaluate the LHS before doing any evaluation of the RHS.
In the code shown, since ++i is -2, the LHS of the || evaluates to true (1) and the RHS is not evaluated. Therefore, neither j nor k is incremented. The printed result follows: m was assigned 1, i became -2, and j stayed as 2 and k stayed as 0.
The only remaining issue is that && binds tighter than ||, so:
a || b && c
is equivalent to:
a || (b && c)
so if a evaluates to true (non-zero), then neither b nor c is evaluated.
Operator precedence is a completely different thing. Order of evaluation is determined by side effects and sequence points.
see this manual on order of evaluation.
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.