nightlytrails
nightlytrails

Reputation: 2662

C/C++ underlying representation of char, unsigned char and signed char

Would I be correct to say that the underlying object representation (bit pattern) in each of the following definitions is the same?

char c = 240;
unsigned char c = 240;
signed char c = 240;

So, the signed-ness matters only when c is used in an expression (or casts)?

Upvotes: 2

Views: 1069

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320631

In general case it is not correct to say that the pattern is the same, if the range of signed char does not cover 240. If 240 is out of range, the result of this overflowing initialization is implementation-defined (and may result in a signal, see 6.3.1.3/3). The same applies to char initialization if it is signed.

The language guarantees matching representations only for the common part of the ranges of signed char and unsigned char. E.g. this is guaranteed to produce the same pattern

char c = 10;
unsigned char c = 10;
signed char c = 10;

With 240 there's no such guarantee in general case (assuming it is out of range).

Upvotes: 2

Related Questions