My original post is rather broad and consists of many specific questions of which I should have given each its own page. However, I address and answer each question here so future visitors can grok the answers more easily.
Answer 1
Question:
- Is this comparison type safe?
The comparison between myarr[2]
and foobar
in this particular case is safe since both variables hold unsigned values. In general, however, this is not true.
For example, suppose an implementation defines char
to have the same behavior as signed char
, and int
is able to represent all values representable by unsigned char
and signed char
.
char foo = -25;
unsigned char bar = foo;
if(foo == bar){
printf("This line of text will not be printed.\n");
}
Although bar
is set equal to foo
, and the C99 standard guarantees that there is no loss of precision when converting from signed char
to unsigned char
(see Answer 2), the foo == bar
conditional expression will evaluate false.
This is due to the nature of integer promotion as required by section 6.3.1 paragraph 2 of the C99 standard:
If an int
can represent all values of the original type, the value is converted to an int
; otherwise, it is converted to an unsigned int
.
Since in this implementation int
can represent all values of both signed char
and unsigned char
, the values of both foo
and bar
are converted to type int
before being evaluated. Thus the resulting conditional expression is -25 == 231
which evaluates to false.
Answer 2
Question:
- Nevertheless, can I safely convert between
unsigned char
and char
, and back, without losing precision and without risking undefined (or implementation-defined) behavior?
You can safely convert from char
to unsigned char
without losing precision (nor width nor information), but converting in the other direction -- unsigned char
to char
-- can lead to implementation-defined behavior.
The C99 standard makes certain guarantees which enable us to convert safely from char
to unsigned char
.
In section 6.2.5 paragraph 15:
The implementation shall define char
to have the same range,
representation, and behavior as either signed char
or unsigned char
.
Here, we are guaranteed that char
will have the same range, representation, and behavior as signed char
or unsigned char
. If the implementation chooses the unsigned char
option, then the conversion from char
to unsigned char
is essentially that of unsigned char
to unsigned char
-- thus no width nor information is lost and there are no issues.
The conversion for the signed char
option is not as intuitive, but is implicitly guaranteed to preserve precision.
In section 6.2.5 paragraph 6:
For each of the signed integer types, there is a corresponding (but different) unsigned
integer type (designated with the keyword unsigned
) that uses the same amount of
storage (including sign information) and has the same alignment requirements.
In 6.2.6.1 paragraph 3:
Values stored in unsigned bit-fields and objects of type unsigned char
shall be
represented using a pure binary notation.
In section 6.2.6.2 paragraph 2:
For signed integer types, the bits of the object representation shall be divided into three
groups: value bits, padding bits, and the sign bit. There need not be any padding bits; there shall be exactly one sign bit. Each bit that is a value bit shall have the same value as
the same bit in the object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M <=
N).
- First,
signed char
is guaranteed to occupy the same amount of storage as an unsigned char
, as are all signed integers in respect to their unsigned counterparts.
- Second,
unsigned char
is guaranteed to have a pure binary representation (i.e. no padding bits and no sign bit).
signed char
is required to have exactly one sign bit, and no more than the same number of value bits as unsigned char
.
Given these three facts, we can prove via pigeonhole principle that the signed char
type has at most one less than the number of value bits as the unsigned char
type. Similarly, signed char
can safely be converted to unsigned char
with not only no loss of precision, but no loss of width or information as well:
unsigned char
has storage size of N
bits.
signed char
must have the same storage size of N bits.
unsigned char
has no padding or sign bits and therefore has N
value bits
signed char
can have at most N
non-padding bits, and must allocate exactly one bit as the sign bit.
signed char
can have at most N-1
value bits and exactly one sign bit
All signed char
bits therefore match up one-to-one to the respective unsigned char
value bits; in other words, for any given signed char
value, there is a unique unsigned char
representation.
/* binary representation prefix: 0b */
(signed char)(-25) = 0b11100111
(unsigned char)(231) = 0b11100111
Unfortunately, converting from unsigned char
to char
can lead to implementation-defined behavior. For example, if char
is defined by the implementation to behave as signed char
, then an unsigned char
variable may hold a value that is outside the range of values representable by a signed char
. In such cases, either the result is implementation-defined or an implementation-defined signal is raised.
In section 6.3.1.3 paragraph 3:
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
Answer 3
Question:
- Does this mean that it is not safe to store an
unsigned
value in a signed
variable of the same type?
Trying to convert an unsigned
type value to a signed
type value can result in implementation-defined behavior if the unsigned
type value cannot be represented in the new signed
type.
unsigned foo = UINT_MAX;
signed bar = foo; /* possible implementation-defined behavior */
In section 6.3.1.3 paragraph 3:
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
An implementation-defined result would be any value returned within the range of values representable by the new signed
type. An implementation could theoretically return the same value consistently (e.g. 42
) for these cases and thus loss information occurs -- i.e. there is no guarantee that converting from unsigned
to signed
to back to unsigned
will result in the same original unsigned
value.
An implementation-defined signal is that which conforms to the rules laid out in section 7.14 of the C99 standard; an implementation is permitted to define additional conforming signals which are not explicitly enumerated by the C99 standard.
In this particular case, an implementation could theoretically raise the SIGTERM
signal which requests the termination of the program. Thus, attempting to convert an unsigned
type value to signed
type could result in a program termination.
Answer 4
Question:
- Does
foo == bar
evaluate to a false value, even if -1
is equivalent to 255
when an explicit (unsigned char
) cast is used?
Consider the following code:
signed char foo = -1;
unsigned char bar = 255;
if((unsigned char)foo == bar){
printf("same\n");
}
Although signed char
and unsigned char
values are promoted to at least int
before the evaluation of a conditional expression, the explicit unsigned char
cast will convert the signed char
value to unsigned char
before the integer promotions occur. Furthermore, converting to an unsigned
value is well-defined in the C99 standard and does not lead to implementation-defined behavior.
In section 6.3.1.3 paragraph 2:
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
subtracting one more than the maximum value that can be represented in the new type
This the conditional expression essentially becomes 255 = 255
which evaluates to true.
until the value is in the range of the new type.
Answer 5
Questions:
- Does this mean that not even an explicit conversion is safe?
In general, an explicit cast to char
for a value outside the range of values representable by signed char
can lead to implementation-defined behavior (see Answer 3). A conversion need not be implicit for section 6.3.1.3 paragraph 3 of the C99 standard to apply.