You are on page 1of 1

1.

14 TYPE CASTING

Te conversion of one data type to another is known as type casting or type conversion.

Tere are two forms of type conversion:

(1) Implicit: In this situation, the compiler casts the values of distinct data types in an

expression to a common type, which is the highest hierarchy.

int → unsigned int → long → unsigned long → long long → unsigned long long → foat

→ double → long double

Example:

Here, because foat data type is higher in the hierarchy between foat and int, the arithme c
expression total/count gives a foat value.

12 ◾ Learn Programming with C

(2) Explicit: In this situation, the programmer uses the cast operator to explicitly

change values of one type to another. Te syntax is as follows:

(type_name) expression

Example:

First, the total is transformed from an int to a foat, and then division is performed. Because

total has been converted to foat and count has been changed to int, the result is a decimal

value because foat is higher in the hierarchy than int data type.

You might also like