Walter Banks said:
Stefan,
Besides Keith's comments.
The "as if" rules is frequently a part of committee discussions both
when they are seated and off line discussions.
It is generally taken to mean that C compilers must behave as
if they were implemented according to the standard but the
implementor is free make implementation choices "as long" as
the resulting behaviour does not change.
Or if the behavior was undefined in the first place.
The most common examples are casting to a larger integer size
does not need to be done if the statement result is the same.
Adding two 8 bit chars and storing the result to a char can
be done without the casts to and from int.
Um, probably.
Suppose plain char is signed, and CHAR_BIT==8. Consider:
char x = 100;
char y = 100;
char z = x + y;
Following the abstract machine semantics, the values of x and y are
converted from char to int via the "usual arithmetic conversions",
then they're added, yielding the int value 200, then that result is
converted to char. Since 200 exceeds CHAR_MAX, "either the result is
implementation-defined or an implementation-defined signal is raised"
(C99 6.3.1.3p3).
If the addition is done in type char, without promotion to int, then
the overflow causes the behavior of the addition to be undefined.
If the undefined behavior, say, causes the program to crash, then
the transformation changes the visible behavior of the program and
is therefore not allowed.
Of course the behavior isn't necessarily undefined as far as the
compiler is concerned. The compiler is free to take advantage of
its knowledge of how an overflowing addition behaves. On a typical
modern system, z will end up with the value -56 regardless of how
it's computed.