问题
This is My Code:
int main()
{
int i=2,j=2,c=2,d;
d= ++i || ++j && c;
printf("I is %d\n",i);
printf("J is %d\n",j);
printf("C is %d\n",c);
printf("D is %d\n",d);
return 0;
}
The output of the following code is:
i is 3
j is 2
c is 2
d is 1
Now my question is, if ++i
is 3
then why ++j
is 2?
What is the difference between ++i
& i++
? Also I want to know that how come d
is 1
?
回答1:
You asked:
Now my question is if ++i is 3 then why ++j is 2?
++j
is never executed since ++i
evaluates to true.
The expression ++i || ++j && c
is equivalent to ++i || (++j && c)
due to operator precedence.
The run time evaluates the expression ++i || ++j && c
from left to right.
The first term, ++i
evaluates to 3
, with the side effect that the value of i
is 3
. Since it is non-zero, the run time does not need to evaluate anything after that. Since ++j
is never evaluated, the value of j
is not changed.
What is the difference between
++i
&i++
?
The value of the expression ++i
is i+1
with the side effect that the value of i
is incremented by 1
.
The value of the expression i++
is i
with the side effect that the value of i
is incremented by 1
.
int i = 2;
int x = ++i; // x == 3, i == 3 at the end of execution of the statement
int y = i++; // y == 3, i == 4 at the end of execution of the statement
I want to know that howcome D is 1
The value of d
is set to 1 since the boolean value of the expression is true
.
回答2:
++i is pre increment.
i++ is post increment.
Suppose here i = 2
In Post In crement
printf("%d",i++); // it will print output 2 then increment value of i to 3
In Pre Increment
printf("%d",++i); //first it will increment value of i to 3, then print output 3
d= ++i || ++j && c;
Condition will be performed in this manner ++i || (++j && c);
As i=2, after ++i
it will become 3
.
In OR if first condition is true it will skip second condition. means ++j && c
will not be performed.
Result of ++i || ++j && c
is 1
so d = 1.
printf("I is %d\n",i); // i = 3;
printf("J is %d\n",j); // j = 2;
printf("C is %d\n",c); // c = 2;
printf("D is %d\n",d); // d = 1;
Short Circuit evaluation
回答3:
Reason for the evaluation is short-circuiting of the boolean operators &&
and ||
.
- For
&&
, if the left-hand side expression is false, the combined result is false (the right-hand side expression is never evaluated). - For
||
, if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated).
This is mostly important when the right-hand expression has side effects, such as altering values(or calling some functions with side-effects), for example in your case:
d= ++i || ++j && c;
Note that the combined conditional expression would increment i
by 1
since the conditional expression on the left of ||
is true, the conditional expression on the right-hand side i.e. ++j && c
is never evaluated.
Thus, the total expression will lead eventually to:
d = 1 || ++j && c;
which explains why d=1?
Now if you change the value of i=-1
then the expression on the right of the ||
will be executed and the j
will be incremented as per your expectation
Now for the performance effects of i++ vs ++i check here :)
来源:https://stackoverflow.com/questions/29998697/difference-between-i-and-i