The C11 standard defines the _Bool
type (6.2.5.2) as a standard unsigned integer type (6.2.5.6) and as I read the standard, _Bool
is then also an arithmetic type (6.2.5.18 via 6.2.5.7 and 6.2.5.17).
Furthermore, it is specified that for
and -
"both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type" (6.5.6.2).
However, about the result I can only see "The result of the binary operator is the sum of the operands" (6.5.6.5) and "The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first" (6.5.6.6). For two booleans, "sum" may be interpreted as logical OR, but I do not think "subtraction" has a well-defined meaning.
So the question is: is the result of a b
and a-b
(where a
and b
have type _Bool
) undefined behavior in C11 or does the standard clearly define the result of these operations (if so, where?)?
Note: maybe the standard just sees _Bool
as an integer with very small range. In that case, I would expect true
true
to be 0
(1 1 modulo 2). However, GCC says 1.
CodePudding user response:
From the C Standard (6.5.6 Additive operators)
4 If both operands have arithmetic type, the usual arithmetic conversions are performed on them
And (6.3.1.8 Usual arithmetic conversions)
1 Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way ...
Otherwise, the integer promotions are performed on both operands.
And (6.3.1.1 Boolean, characters, and integers)
- ...If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. 58) All other types are unchanged by the integer promotions.
So the result of an additive operation has the type int
when the both operands have the type _Bool
that are integer promoted to the type int
before performing the operation.
Pay attention to that in C there is no boolean type as in C . The boolean type _Bool
is a standard unsigned integer type in C that can store either 1
or 0
.
So if you will write for example
_Bool a = 1;
_Bool b = 1;
_Bool c = a b;
then the value of the variable c will be 1 because any non-zero value (and the result of the operation is the value 2 of the type int
) is converted to 1.
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.