Type conversion in C language
Type conversion in C language refers to the process of converting a value of one data type into another data type. There are two types of type conversion: implicit and explicit.
Implicit Type Conversion:
Implicit type conversion is also known as automatic type conversion. It occurs when the compiler automatically converts one data type into another data type without any explicit instruction from the programmer. Implicit type conversion occurs when:
A value of a smaller data type is assigned to a variable of a larger data type.
A value of a lower-ranked data type in the type hierarchy is assigned to a variable of a higher-ranked data type.
float b=a; // Implicit Conversion
In the above example, the integer value 10 is automatically converted to a floating-point value and assigned to the variable b.
Explicit Type Conversion:
Explicit type conversion is also known as type casting. It occurs when the programmer explicitly instructs the compiler to convert a value of one data type into another data type.
Explicit type conversion is done using the cast operator, which is represented by parentheses ().
int a = (int)
In the above example, the floating-point value 10.5 is explicitly converted to an integer value using the cast operator and assigned to the variable a.
Note that explicit type conversion can result in data loss if the value being converted cannot be represented by the target data type. It is the responsibility of the programmer to ensure that the data being converted is within the range of the target data type.