Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A condition evaluates to signed or unsigned integer?

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?

like image 967
user1969104 Avatar asked Nov 03 '15 12:11

user1969104


People also ask

How do you check if an integer is signed or unsigned?

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.

How do you know if a variable is signed or unsigned?

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).

What is signed and unsigned variable?

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.

What is signed or unsigned in C?

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.


2 Answers

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 yield 1 if the specified relation is true and 0 if it is false. The result has type int.

So the type of 1 - (a > b) is also int, a signed type.

like image 154
Yu Hao Avatar answered Sep 27 '22 22:09

Yu Hao


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.

like image 27
Bathsheba Avatar answered Sep 27 '22 21:09

Bathsheba