Q1:输出?
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;
}
Q2:输出?
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;
}
第三季度:输出?
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;
}
请解释一下这项操作是如何运作的?
答案 0 :(得分:2)
布尔运算从左到右处理(没有括号......)。一旦结果得到修复,它就会停止评估其余的条件。这意味着:
false && (AND) -> is always false, no matter what
true || (OR) -> is always true, no matter what
first case stops @ || (because true && true || doesn't matter)
second case stops @ || (because true || doesn't matter && how much && operations ...)
third case stops @ end (because true && true && -> still have to check because if there would be false, the whole expression would be false)
因为它在特定点停止,所以不会为其余的东西执行++运算符。
这也是为什么你必须小心处理像incr,decr或某些条件的东西......