Standard Conversions

Standard conversions are built in the mikroC PRO for PIC32. 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 PIC32 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-fills the extra bits of the new value, depending on whether the shorter type is signed or unsigned, respectively.

  Note : Conversion of floating point data into integral value (in assignments or via explicit typecast) produces correct results only if the float value does not exceed the scope of destination integral type.

In details:

Here are the steps the mikroC PRO for PIC32 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-filled
  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 modifiers) or float (equivalent with double and long double in the mikroC PRO for PIC32).

  1. If either operand is float, the other operand is converted to float.
  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.

Here are several examples of implicit conversion:

2 + 3.1      /* → 2. + 3.1 → 5.1 */
5 / 4 * 3.   /* → (5/4)*3. → 1*3. → 1.*3. → 3. */
3. * 5 / 4   /* → (3.*5)/4 → (3.*5.)/4 → 15./4 → 15./4. → 3.75 */

Pointer Conversions

Pointer types can be converted to other pointer types using the typecasting mechanism:

char *str;
int *ip;
str = (char *)ip;

More generally, the cast type* will convert a pointer to type “pointer to type”.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code