User manual
mikroC PRO for dsPIC
MikroElektronika
223
Standard Conversions
Standard conversions are built in the mikroC PRO for dsPIC30/33 and PIC24. These conversions are performed
automatically, whenever required in the program. They can also be explicitly required by means of the typecast operator
(refer to the Explicit Typecasting).
The basic rule of automatic (implicit) conversion is that the operand of simpler type is converted (promoted) to the type
of more complex operand. Then, the type of the result is that of more complex operand.
Arithmetic Conversions
When using arithmetic expression, such as a + b, where a and b are of different arithmetic types, the mikroC PRO
for dsPIC30/33 and PIC24 performs implicit type conversions before the expression is evaluated. These standard
conversions include promotions of “lower” types to “higher” types in the interests of accuracy and consistency.
Assigning a signed character object (such as a variable) to an integral object results in automatic sign extension.
Objects of type signed char always use sign extension; objects of type unsigned char always has its high byte set to
zero when converted to int.
Converting a longer integral type to a shorter type truncates the higher order bits and leaves low-order bits unchanged.
Converting a shorter integral type to a longer type either sign-extends or zero-lls the extra bits of the new value,
depending on whether the shorter type is signed or unsigned, respectively.
Note: Conversion of oating point data into integral value (in assignments or via explicit typecast) produces correct
results only if the oat value does not exceed the scope of destination integral type.
In details:
Here are the steps the mikroC PRO for dsPIC30/33 and PIC24 uses to convert the operands in an arithmetic
expression:
First, any small integral types are converted according to the following rules:
1. bit converts to char
2. char converts to int
3. signed char converts to int, with the same value
4. short converts to int, with the same value, sign-extended
5. unsigned short converts to unsigned int, with the same value, zero-lled
6. enum converts to int, with the same value
After this, any two values associated with an operator are either int (including the long and unsigned modiers) or
oat (equivalent with double and long double in the mikroC PRO for dsPIC30/33 and PIC24).
1. If either operand is oat, the other operand is converted to oat.
2. Otherwise, if either operand is unsigned long, the other operand is converted to unsigned long.
3. Otherwise, if either operand is long, then the other operand is converted to long.
4. Otherwise, if either operand is unsigned, then the other operand is converted to unsigned.
5. Otherwise, both operands are int.
The result of the expression is the same type as that of the two operands.