It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a code below and wanted to know what could be output.What i would like to see how different compiler would interpret this particular piece of code.
int main()
{
int i = -1, j = 2, k = 0, m;
m = ++i || ++j && ++k;
printf("\n %d %d %d %d \n", i, j, k, m);
return 0;
}
Now the question is while handling this code would compiler go by the rule which says
This expresion can be seen as
m = ++i || (++j && ++k);
as && has higher precedence over || and the result would be 0 2 0 1
or
This expresion can be seen as
m = ++i || (++j && ++k);
but compiler still will try to short circuit. so it evaluates ++i, since it's 1,(++j&&++k) >are not evaluated.so ans is 0 3 1 1
The expression ++i || ++j && ++k will group like so due to precedence:
++i || (++j && ++k)
One of the few rules C has about evaluation order of expressions is that the left-hand side of the || and the `&& operators must execute before the right-hand side (for short circuiting). This is a non-optional language requirement.
That means that ++i has to be evaluated first. It will return 0 (it does not evaluate to 1 as you mistakenly mention in the question). Since that's not enough to short circuit the || operation, the right side will need to be evaluated. A similar process will occur for evaluation the && operator (and both sides of the && operation will need to be evaluated).
The resulting output will be:
0 3 1 1
regardless of the compiler. There is no undefined, unspecified, or implementation defined behavior evaluating that expression.
The expression ++i || ++j && ++k will group like so due to precedence:
++i || (++j && ++k)
One of the few rules C has about evaluation order of expressions is that the left-hand side of the || and the `&& operators must execute before the right-hand side (for short circuiting). This is a non-optional language requirement.
That means that ++i has to be evaluated first. It will return 0. Since that's not enough to short circuit the || operation, the right side will need to be evaluated. A similar process will occur for evaluation the && operator (and both sides of the && operation will need to be evaluated).
The resulting output will be:
0 3 1 1
regardless of the compiler. There is no undefined, unspecified, or implementation defined behavior evaluating that expression.
One way to get the output you mention in your question is to use the following expression instead:
m = --i || ++j && ++k;
In this case, --i evaluates to -2 and the short circuiting rule requires that the right side of the || operator not be evaluated. So j and k are left alone.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
int i=3,j=1,k=0,m;
m=++i || ++j && ++k;
printf("%d%d%d%d",i,j,k,m);
//output is 4 1 0 1enter code here
//can anyone explain why k=0 and j=1 only
m = ++i || ++j && ++k; is grouped as m = ++i || (++j && ++k); since, && has higher precedence. But, they are evaluated from left to right.
Since, ++i = 4, which a non-zero number, the right hand expression is not evaluated. I mean (++j && ++k) is not evaluated since, left hand expression result is non-zero.
For A||B, if A = 1, then results is always 1, irrespective of the value of B.
Since, the right hand expression is not evaluated, the values of j and k remain same.
This feature is called "Short Circuit Evaluation".
Precedence and associativity only affect how operators are grouped with operands - they do not affect the order in which expressions are evaluated. Both || and && force left-to-right evaluation. Also, remember that the && and || operators short-circuit as follows:
In the expression a || b, b will only be evaluated if a evaluates to zero
In the expression a && b, b will only be evaluated if a evaluates to non-zero
The expression
++i || ++j && ++k
is parsed as
(++i) || ((++j) && (++k))
and is evaluated as follows:
++i is fully evaluated and all side effects applied;
The result of ++i is 4, which is non-zero;
The || does not evaluate the right operand if the left operand is non-zero, so ++j && ++k is not evaluated at all;
When everything is said and done, only i is updated, j and k are not.
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:
Behaviour of && in C programming language
(5 answers)
Closed 9 years ago.
Here's my program:
int main(void)
{
int i, j, k, m;
i=-3, j=2, k=0;
m = k++ && ++i && ++j;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
The output of the program above is:
-3 2 1 0
But according to operator precedence table, ++ should have evaluated first I guess.
I also tried putting parenthesis around them, but still the output remains same.
I read somewhere that putting parenthesis around will make it evaluate first, no matter what.
Somebody, please make it clear, how it's evaluated.
Thanks in advance.
What's happening here is short-circuiting. k++ evaluates to 0, which is false. Thus the entire boolean expression k++ && ++i && ++j is false, so ++i and ++j are never executed.
You are confusing precedence with order of evaluation. Precedence defined how the operands are grouped. The higher precedence of ++ makes the expression equivalent to:
m = (k++) && (++i) && (++j);
But the evaluation order is irrelevant. The shortcut circuit of && guarantees
that its left-hand operand is evaluated first.
In general, most operator doesn't specify the order of evaluation, with four exceptions: logical AND &&, logical OR||, conditional operator ?: and comma operator ,.
As a concrete example of different order of evaluation:
Given int i = 0, the result of i + (i++) is unspecified, the compiler may evaluates i++ first, which modifies the value of i, the compiler may choose to evaluates i first. You should avoid expressions like these.
On the other hand, the result of i && (i++) is determined, as && ensures the left operand i is evaluated first, since it's zero, the right operand i++ is never evaluated.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
Since ++ has more precedence than || and && in C, they are evaluated first and therefore the expression becomes m = -2 || 3 && 1. Now you can apply short circuiting but that produces incorrect answer. Why is that?
Precedence ≠ order of evaluation.
The short-circuiting behavior of || and && means that their left-hand sides are evaluated first, and
If the LHS of || evaluates to true (nonzero), the RHS is not evaluated (because the expression will be true no matter what the RHS is)
If the LHS of && evaluates to false (or zero), the RHS is not evaluated (because the expression will be false no matter what the RHS is)
In your example, ++i gets evaluated, and is equal to -2, which is nonzero, so the right-hand side of the || (that is, ++j && ++k) never gets evaluated: j and k are never incremented.
The ++s don't execute before the expression. Only ++i executes, which indicates that the result of the expression will be 1, therefore the rest of the expression is not evaluated (short circuit).
Your code is equivalent to:
if (++i)
m = 1;
else
if (!++j)
m = 0;
else if (!++i)
m = 0;
else
m = 1;
This means that once ++i is evaluated to true, the else part is never executed.
This question already has answers here:
Precedence of && over || [duplicate]
(4 answers)
Closed 8 years ago.
I don't understand the output of the following program:
#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;
}
The output is -2 2 0 1 instead of -2 3 1 1, implying that ++i was evaluated (and caused the || operator to short-circuit its right hand side) before the expression ++j && ++k which appears to contradict the fact that the && operator has higher precedence than ||.
Would someone explain why?
The output should be something like:
Error, line 2: 'm': undefined variable.
Edit: with that fixed, only the ++i should be evaluated. Precedence does not determine (or even affect) order of evaluation. Precedence means the expression is equivalent to ++i || (++j && ++k). Order of evaluation for || or && is always that the left operand is evaluated, then there's a sequence point. After the sequence point, the right operand is evaluated if and only if necessary to determine the final result (i.e., the right operand of || is evaluated if the left operand evaluated to zero; the right operand of && is evaluated if the left operand evaluated to non-zero).
In this expression, ++i is evaluated, then because it's the left operand of || and evaluated non-zero, none of the rest of the expression is evaluated.
-2 2 0 1
Lazy.
#include <stdio.h>
int main()
{
int i=-3,j=2,k=0;
int m=++i||++j&&++k;
printf("%d %d %d %d",i,j,k,m);
}
Compile, run and see for youself.
gcc tmp.c -o tmp
The expression:
++i || ++j && ++k
Is equivalent to:
(++i) || ((++j) && (++k))
Explaining:
++i is evaluated -- (-2) || ((++j) && (++k));
The || operator is evaluated -- (1);
Since 1 || anything evalutes true, the right operand is not evaluated. Thus, the && precedence doesn't matter here. This short circuiting is guaranteed in both C and C++ by the relevant standards (see Is short-circuiting logical operators mandated? And evaluation order?).
Now, try using a sub-expression, like this:
(++i || ++j) && ++k
Which is equivalent to:
((++i) || (++j)) && (++k)
Explaining:
++i is evaluated -- ((-2) || (++j)) && (++k);
|| is evaluated -- (1) && (++k)
++k is evaluated -- (1) && (1);
Evaluates true;
By way of explanation:
#include <stdio.h>
int main()
{
if (-1)
printf ("-1!\n");
else
printf ("Not -1.\n");
return 0;
}
Negative numbers are not false in C. Always compare boolean values to 0 (or FALSE) or you can get bitten by if (worked == TRUE) giving false negatives.