Types conversions, Standard conversions, Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 90

Advertising
background image

C is strictly typed language, with each operator, statement and function demanding
appropriately typed operands/arguments. However, we often have to use objects of
“mismatching” types in expressions. In that case, type conversion is needed.
Conversion of object of one type is changing it to the same object of another type
(i.e. applying another type to a given object). C defines a set of standard conver-
sions for built-in types, provided by compiler when necessary.

Conversion is required in following situations:

- if statement requires an expression of particular type (according to language

definition), and we use an expression of different type,

- if operator requires an operand of particular type, and we use an operand of

different type,

- if a function requires a formal parameter of particular type, and we pass it an

object of different type,

- if an expression following the keyword return does not match the declared

function return type,

- if intializing an object (in declaration) with an object of different type.

In these situations, compiler will provide an automatic implicit conversion of
types, without any user interference. Also, user can demand conversion explicitly
by means of typecast operator. For more information, refer to Explicit
Typecasting.

Standard Conversions

Standard conversions are built in C. These conversions are performed automatical-
ly, whenever required in the program. They can be also explicitly required by
means of typecast operator (refer to 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, type of
the result is that of more complex operand.

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

82

MikroElektronika: Development tools - Books - Compilers

page

TYPES CONVERSIONS

Advertising
This manual is related to the following products: