You are here

Implicit Type Conversion

5 February, 2015 - 15:05

Automatic conversion of a value from one data type to another by a programming language, without the programmer specifcally doing so, is called implicit type conversion. It happens when ever a binary operator has two operands of different data types. Depending on the operator, one of the operands is going to be converted to the data type of the other. It could be promoted or demoted depending on the operator.

Example 3.5: Implicit Promotion

55 + 1.75

In this example the integer value 55 is converted to a floating-point value (most likely double) of 55.0. It was promoted.

Example 3.6: Implicit Demotion

int money;     // variable set up     then later in the program money = 23.l6; 

In this example the variable money is an integer. We are trying to move a floating-point value 23.16 into an integer storage location. This is demotion and the floating-point value usually gets truncated to 23.