的逻辑或执行用C顺序 [英] Order of logical OR execution in C
问题描述
在想,如果下一个语句可能导致保护故障等可怕的东西,如果接下来的值为NULL(节点是链表)。
Was wondering if the next statement could lead to a protection fault and other horrible stuff if value of next is NULL(node being a linked list).
if (!node->next || node->next->some_field != some_value) {
一旦第一部分为真
林假定OR的第二部分不进行评价。我错在假设这?这是编译器特定的?
Im assuming the second part of the OR is not evaluated once the first part is true. Am I wrong in assuming this? Is this compiler specific?
推荐答案
在ISO-IEC-9899-1999标准(C99),第6.5.14:
In the ISO-IEC-9899-1999 Standard (C99), Section 6.5.14:
该||经营者应当产生1如果任其操作数比较不等
为0;否则,它产生0结果的类型为int。 4不同于
按位|操作者,在||左到右的运营商保证评价;
还有就是第一个操作数的评估后的序列点。
如果第一操作数进行比较不等于0,第二个操作数不是
评估。
The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int. 4 Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated.
这是不是编译器特定的。如果于节点>接下来
是 NULL
,则条件的其余部分的从不评估。
This is not compiler-specific. If node->next
is NULL
, then the rest of the condition is never evaluated.
这篇关于的逻辑或执行用C顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!