We know that a condition in C, for example a > b
, results in either 0 or 1. If we would like to utilize this 0
/1
value directly in expression, e.g. 1 - (a > b)
, should we assume that it is signed or unsigned (as it can make a difference in expressions)? What does standard say about this?
Variables such as integers can be represent in two ways, i.e., signed and unsigned. Signed numbers use sign flag or can be distinguish between negative values and positive values. Whereas unsigned numbers stored only positive numbers but not negative numbers.
A numeric variable is signed if it can represent both positive and negative numbers, and unsigned if it can only represent non-negative numbers (zero or positive numbers).
The term "unsigned" in computer programming indicates a variable that can hold only positive numbers. The term "signed" in computer code indicates that a variable can hold negative and positive values. The property can be applied to most of the numeric data types including int, char, short and long.
The int type in C is a signed integer, which means it can represent both negative and positive numbers. This is in contrast to an unsigned integer (which can be used by declaring a variable unsigned int), which can only represent positive numbers.
The type of the result of all relational operators is int
:
C11 §6.5.8 Relational operators
Each of the operators
<
(less than),>
(greater than),<=
(less than or equal to), and>=
(greater than or equal to) shall yield1
if the specified relation is true and0
if it is false. The result has type int.
So the type of 1 - (a > b)
is also int
, a signed type.
The standard is clear. All relational operators (including equality and not equality) evaluate to 0 or 1, which are int
types.
a > b
will evaluate to 1 if a
is bigger than b
, and 0 otherwise.
1 - (a > b)
therefore is an expression with type int
.
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