gpt4 book ai didi

c - if 语句中 bool 表达式的顺序是否固定?

转载 作者:太空狗 更新时间:2023-10-29 16:10:54 24 4
gpt4 key购买 nike

这个if语句中boolean表达式的顺序是固定的吗?

if(boolean_expression_1 || boolean_expression_2) {

}

boolean_expression_1 是否总是在 boolean_expression_2 之前求值?求值顺序是 C 中的标准吗?

最佳答案

是的。这是有保证的。它叫做"short circuit"评价。

来自 C11 草案,6.5.14 逻辑或运算符:

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.

(强调我的)。

关于c - if 语句中 bool 表达式的顺序是否固定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37571798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com