C 标准将 _Bool 定义为包含 0 或 1 的无符号类型。如果 _Bool 类型的值 1 递增,据我所知,有两个选项:
在我的系统上的 GCC 和 Clang 上,行为似乎是后者。这是否由标准明确定义?
最佳答案
来自 C 标准(6.3.1.2 boolean 类型)
1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
例如 6.5.3.1 前缀自增和自减运算符
2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1).
最后 6.5.16.2 复合赋值
3 A compound assignment of the form E1 op = E2 is equivalent to the simple assignment expression E1 = E1 op (E2), except that the lvalue E1 is evaluated only once, and with respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation. If E1 has an atomic type, compound assignment is a read-modify-write operation with memory_order_seq_cst memory order semantics.
注意(6.3 转换)
— The rank of _Bool shall be less than the rank of all other standard integer types.
因此在表达式中使用的类型 _Bool 被转换为具有更高等级的其他类型。
关于c - 是否定义了 _Bool 递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62119285/