Type conversion in C

Typecast is basically a conversion from one type to another.

There are two types of type conversion.

  1. Implicit type conversion (Type conversion)

  2. Explicit type conversion (Typecasting)



1). Implicit type conversion (type conversion)

Implicit type conversion is also known as automatic type conversion, Done by the compiler on its own generally takes place when in an expression more then one data type is present in such condition type conversion (Promotion) takes place to avoid loss of data.

All the data types of the variables are upgrades to the data type of the variable with the largest data type.

boll-> char -> short int -> int -> unsigned int -> long -> Unsigned -> long long -> float -> double -> long double

It is possible for implicit conversions to lose information signs can be lost  (When signed is implicitly converted to unsigned) and overflow can occur ( When long long is implicitly converted to float)



2). Explicit type conversion (Type casting)

This process is also called type casting and it is user-defined here the user can typecast the result to make it of a particular data type.

Syntex

(type) expression


Example:

 int x=20;

float y = (float)x;




Advantages of type conversion

This is done to take advantage of certain features of type hierarchies or type representations.

It helps us to compute expressions containing variables of different data types.