Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 Benutzerhandbuch
Produktcode
SW006021-1
C Language Features
2012 Microchip Technology Inc.
DS52053B-page 179
5.6
OPERATORS AND STATEMENTS
The MPLAB XC8 C Compiler supports all the ANSI operators. The exact results of
some of these are implementation defined. Implementation-defined behavior is fully
documented in Appendix C. “Implementation-Defined Behavior”. The following
sections illustrate code operations that are often misunderstood as well as additional
operations that the compiler is capable of performing.
some of these are implementation defined. Implementation-defined behavior is fully
documented in Appendix C. “Implementation-Defined Behavior”. The following
sections illustrate code operations that are often misunderstood as well as additional
operations that the compiler is capable of performing.
5.6.1
Integral Promotion
When there is more than one operand to an operator, they typically must be of exactly
the same type. The compiler will automatically convert the operands, if necessary, so
they do have the same type. The conversion is to a “larger” type so there is no loss of
information; however, the change in type can cause different code be ha vi our to what
is sometimes expected. These form the standard type conversions.
the same type. The compiler will automatically convert the operands, if necessary, so
they do have the same type. The conversion is to a “larger” type so there is no loss of
information; however, the change in type can cause different code be ha vi our to what
is sometimes expected. These form the standard type conversions.
Prior to these type conversions, some operands are unconditionally converted to a
larger type, even if both operands to an operator have the same type. This conversion
is called integral promotion and is part of Standard C be ha vi our. The compiler per-
forms these integral promotions where required, and there are no options that can con-
trol or disable this operation. If you are not aware that the type has changed, the results
of some expressions are not what would normally be expected.
larger type, even if both operands to an operator have the same type. This conversion
is called integral promotion and is part of Standard C be ha vi our. The compiler per-
forms these integral promotions where required, and there are no options that can con-
trol or disable this operation. If you are not aware that the type has changed, the results
of some expressions are not what would normally be expected.
Integral promotion is the implicit conversion of enumerated types, signed or
unsigned
unsigned
varieties of char, short int or bit-field types to either signed int or
unsigned int
. If the result of the conversion can be represented by an signed int,
then that is the destination type, otherwise the conversion is to unsigned int.
Consider the following example.
unsigned char count, a=0, b=50;
if(a - b < 10)
count++;
The unsigned char result of a - b is 206 (which is not less than 10), but both a and
b
b
are converted to signed int via integral promotion before the subtraction takes
place. The result of the subtraction with these data types is -50 (which is less than 10)
and hence the body of the if() statement is executed.
and hence the body of the if() statement is executed.
If the result of the subtraction is to be an unsigned quantity, then apply a cast. For
example:
example:
if((unsigned int)(a - b) < 10)
count++;
The comparison is then done using unsigned int, in this case, and the body of the
if()
if()
would not be executed.
Another problem that frequently occurs is with the bitwise compliment operator, ~. This
operator toggles each bit within a value. Consider the following code.
operator toggles each bit within a value. Consider the following code.
unsigned char count, c;
c = 0x55;
if( ~c == 0xAA)
count++;
If c contains the value 0x55, it often assumed that ~c will produce 0xAA; however, the
result is 0xFFAA and so the comparison in the above example would fail. The compiler
may be able to issue a mismatched comparison error to this effect in some circum-
stances. Again, a cast could be used to change this behavior.
result is 0xFFAA and so the comparison in the above example would fail. The compiler
may be able to issue a mismatched comparison error to this effect in some circum-
stances. Again, a cast could be used to change this behavior.