shengy
shengy

Reputation: 9749

unsigned and signed values in C

here is the question:

in the Book 'Computer Systems: A Programmer’s Perspective' section 2.2.5

it said that if an unsigned value is comparing with a signed value, all values will be compared in the unisnged format. like this

if(-1 < 0u)
{
    // will not print this line because -1 will be translated to 255.
    printf("all changed to unsigned format");
}

I tried this code in VC6 SP6, the string was NOT outputed. And everything looks good because we all know that -1 was translated to 255.

but when I read the book 'Expert C Programming Deep C Secrets' section 1.10

It said that if my complier uses the ANSI C Standard, this code will print "-1 < (unsigned char)1: ANSI":

if(-1 < (unsigned char)1)
{
    printf("-1 < (unsigned char)1: ANSI");
}
else
{
    printf("-1 NOT Less than (unsigned char)1: K&R");    
}

The output I got was: -1 < (unsigned char)1: ANSI.

I'm using the VC6 SP6 complier.

and why is this happening?

according to the book 'Computer Systems: A Programmer’s Perspective'

-1 < (unsigned char)1 will make -1 be translated as an unsigned value. so it will become sth like this:

255 < 1

and this should not print out the line -1 < (unsigned char)1: ANSI.

can anybody tell me why this is happening?

Upvotes: 2

Views: 2121

Answers (1)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132974

if(-1 < (unsigned char)1)

In this case both operands are promoted to int.

if( -1 < 0u )

In this case both operands are converted to unsigned int.

The following quotes prove me right.

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
— If either operand is of type long double, the other shall be converted to long double.
— Otherwise, if either operand is double, the other shall be converted to double.
— Otherwise, if either operand is float, the other shall be converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on both operands.54)
— Then, if either operand is unsigned long the other shall be converted to unsigned long.
— Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
— Otherwise, if either operand is long, the other shall be converted to long.
— Otherwise, if either operand is unsigned, the other shall be converted to unsigned. [Note: otherwise, the only remaining case is that both operands are int ]

And this (integral promotions):

An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

To be perfectly honest, the quotes are from the C++03 standard, but they hold for C as well.

Upvotes: 12

Related Questions