A _Bool is defined by the C standard to be an unsigned type containing either 0 or 1. If a value of 1 of type _Bool is incremented, there are, as far as I can see, two options:
On GCC and Clang on my system, the behaviour seems to be the latter. Is this well-defined by the standard?
From the C Standard (6.3.1.2 Boolean type)
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.
And for example 6.5.3.1 Prefix increment and decrement operators
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).
And at last 6.5.16.2 Compound assignment
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.
Pay attention to that (6.3 Conversions)
— The rank of _Bool shall be less than the rank of all other standard integer types.
So used in expressions the type _Bool is converted to other types with a greater rank.
As the __Bool converted to the scalar can have value of 0 or 1, the actual increment operation is an equivalent to
__Bool x = false;
int v = x;
v = !!(v + 1);
x = v;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With