![]() |
Data Type Conversion Type Casting Implicit and Explicit in C |
Data Type Conversion(Type Casting) in C Language
Data Type Casting: A type cast is
basically a conversion from one type to another.
Type conversion is another name for type casting. For instance, if a programmer wants to store a long variable value into some simple integer in a program, then they can type cast this long into the int.
There are two types of type conversion
1.
Implicit Type conversion
2.
Explicit Type conversion
Implicit
Type Conversion: Also known as ‘automatic type
conversion’. Done by the compiler on its own, without any external trigger from
the user.
Generally takes place when in an expression more than one
data type is present. In such conditions type conversion (type promotion) takes
place to avoid loss of data.
All the data types of the variables are upgraded to the data
type of the variable with the most significant data type.
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 is implicitly converted to float).
Rules for Implicit Type Casting
·
When different operands of an
expression are of different types, the compiler performs implicit type conversion to
match them. Below are the rules followed by the compiler to perform the implicit conversion.
· Integer Promotion: All types lower than int (char, short, etc) are first promoted to int.
1. If the types of operands differ even after integer promotion, then the following actions are taken
2. If one of the operands is long double, convert all others to long double
3. If one of the operands is double, convert all others to double
4. If one of the operands is float, convert all others to float
5. If one of the operands is float, convert all others to float
6. This operation proceeds from the highest type to the lowest.
7. The order of the data types from highest to lowest is given below:
long double > double > float > unsigned long long > long long > unsigned long > long > unsigned int > int |
#include<stdio.h> int main() { double x = 1.2; int sum = (int)x + 1; printf("sum = %d", sum); return 0; } *****OUTPUT***** sum = 2 |
Explicit Type Conversion: This process is also called type casting and is user-defined. Here the user can type cast the result to make it of a particular data type.
Write a Program Explicit Type Casting in C Language
#include<stdio.h> int main() { double x = 1.2; int sum = (int)x + 1; printf("sum = %d", sum); return 0; } *****OUTPUT***** sum = 2 |
Advantages of Type Casting 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.
Read Also Post Others Programming Language
Introduction to C Programming Language
Introduction to C Programming Language
Introduction to C++ Programming Language
Introduction to Python Programming Language
Introduction to Java Programming Language
Console Input/Output Function In C Programming Language
Data Types in C Language With Example