Zilog EZ80F916 User Manual
Page 91
UM014423-0607
Using the Integrated Development Environment
ZiLOG Developer Studio II
eZ80Acclaim!
®
User Manual
71
When the Disable ANSI Promotions check box is deselected, the compiler performs inte-
ger type promotions when necessary so that the program’s observed behavior is as defined
by the ANSI C Standard. Integer type promotions are conversions that occur automatically
when a smaller (for example, 8 bits) variable is used in an expression involving larger (for
example, 24 bits) variables. For example, when mixing chars and ints in an expression, the
compiler casts the chars into ints. Conversions of this kind are always done, regardless of
the setting of the Disable ANSI Promotions check box.
The ANSI Standard has special rules for the handling of chars (and shorts), and it is the
application of these special rules that is disabled when the check box is selected. The spe-
cial rules dictate that chars (both signed and unsigned) must always be promoted to ints
before being used in arithmetic or relational (such as < and ==) operations. By selecting
the ANSI Promotions check box, these rules are disregarded, and the compiler can operate
on char entities without promoting them. This can make for smaller code because the com-
piler does not have to create extra code to do the promotions and then to operate on larger
values. In making this a deprecated feature, ZiLOG has worked to make the compiler
more efficient at avoiding truly needless promotions so that the code size penalty for
observing the standard is negligible.
Disabling the promotions can often be a safe optimization to invoke, but this is subject to
several exceptions. One exception is when an arithmetic overflow of the smaller variable
is possible. (For example, the result of adding
(char)10
to
(char) 126
does not fit
within an 8-bit
char
variable, so the result is
(char) -120
.) In such cases, you get dif-
ferent results depending on whether ANSI promotions are enabled or disabled. If you
write
char a = 126;
char b = 10;
int i = a + b:
then with ANSI promotions enabled, you get the right answer: 136. With ANSI promo-
tions disabled, you get the wrong answer:
-
120. The reason for the different result is that
while in both cases there is a conversion from
char
to
int
, the conversion is applied ear-
lier or later depending on this setting. With ANSI promotions enabled, the conversion is
done as soon as possible, so it occurs before the addition, and the result is correct even
though it is too large to fit into a
char
. With ANSI promotions disabled, the conversion is
not done until a larger type is explicitly called for in the code. Therefore, the addition is
done with
char
s, the overflow occurs, and only after that is the result converted to
int
.
By the ANSI Standard, these special promotions are only applied to
char
s and
short
s. If
you have the analogous code with the sum of two
int
s being assigned into a
long
, the
compiler does not automatically promote the ints to longs before adding them, and if the
sum overflows the
int
size, then the result is always wrong whether ANSI promotions are
in effect or not. In this sense, the ANSI promotions make the handling of char types incon-
sistent compared to the treatment of other integer types.
It is better coding practice to show such promotions explicitly, as in the following:
int i = (int) a + (int) b;