Microchip Technology SW006023-1N 数据表
MPLAB
®
XC32 C/C++ COMPILER
USER’S GUIDE
2012 Microchip Technology Inc.
DS51686E-page 117
Chapter 8. Operators and Statements
8.1
INTRODUCTION
The MPLAB XC32 C/C++ Compiler supports all ANSI operators. The exact results of
some of these are implementation defined. Implementation-defined behavior is fully
documented in Appendix 18. “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 18. “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.
8.2
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 behavior 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 behavior 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 behavior. The MPLAB XC32
C/C++ Compiler performs these integral promotions where required, and there are no
options that can control 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 behavior. The MPLAB XC32
C/C++ Compiler performs these integral promotions where required, and there are no
options that can control 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.