-7

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; 
} 

Q3:输出?

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; 
}

请解释这个操作是如何工作的?

4

1 回答 1

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 之类的事情的原因……

于 2013-06-26T07:40:15.837 回答