Type conversion in C is the process of converting one data type to another. In type conversion, the destination data type can’t be smaller than the source data type. Type conversion is done at compile time. Type conversion is performed by compiler.
Type conversion means, conversion of one type to another. In C, there are two types of type conversion:
- Implicit type conversion
- Explicit type conversion
Implicit Type Conversion:
The compiler performs this type of
conversion on its own. It is also known as “Automatic type conversion”. In implicit type conversion,
the value of one type is automatically converted to the value of another type. The compiler does it when there is
more than one data type. Here either promotion or demotion happens. The
promotion means conversion to a higher type and demotion means conversion to a
lower type.
When the type conversion is performed automatically by the compiler without programmer’s intervention, such type of conversion is known as implicit type conversion or type promotion.
Programming Example:
Output:
In the above code, the operand ‘a’ is int type and operand ‘b’ is float type. As there are two data types in expression, the compiler converts the result into a higher data type. The result is being stored in float type.
Explicit Type Conversion:
This process
is also called type casting. In explicit type conversion, we manually convert
values of one data type to another
type.
The
syntax of type casting is:
(data_type) expression;
data_type is any data type of C in which we want to convert the output of an expression.
Programming Example:
1.
Output:
2.
0 Comments
if you have any doubts plz let me know...