Javatpoint Logo
Javatpoint Logo

Difference Between Type Casting and Type Conversion

The two terms type casting and the type conversion are used in a program to convert one data type to another data type. The conversion of data type is possible only by the compiler when they are compatible with each other. Let's discuss the difference between type casting and type conversion in any programming language.

What is a type casting?

When a data type is converted into another data type by a programmer or user while writing a program code of any programming language, the mechanism is known as type casting. The programmer manually uses it to convert one data type into another. It is used if we want to change the target data type to another data type. Remember that the destination data type must be smaller than the source data type. Hence it is also called a narrowing conversion.

Difference Between Type Casting and Type Conversion

Syntax:

Target_datatype: It is the data type in which we want to convert the destination data type. The variable defines a value that is to be converted in the target_data type. Let's understand the concept of type casting with an example.

Suppose, we want to convert the float data type into int data type. Here, the target data type is smaller than the source data because the size of int is 2 bytes, and the size of the float data type is 4 bytes. And when we change it, the value of the float variable is truncated and convert into an integer variable. Casting can be done with a compatible and non-compatible data type.

Let's understand the type casting through a C program.

AreaOfRectangle.c

Output:

Difference Between Type Casting and Type Conversion

What is type conversion?

If a data type is automatically converted into another data type at compile time is known as type conversion. The conversion is performed by the compiler if both data types are compatible with each other. Remember that the destination data type should not be smaller than the source type. It is also known as widening conversion of the data type.

Difference Between Type Casting and Type Conversion

Let's understand the type conversion with an example.

Suppose, we have an int data type and want to convert it into a float data type. These are data types compatible with each other because their types are numeric, and the size of int is 2 bytes which is smaller than float data type. Hence, the compiler automatically converts the data types without losing or truncating the values.

In the above example, the int data type is converted into the float, which has a larger size than int, and hence it widens the source data type.

Let's understand type conversion through a C program.

Output:

Difference Between Type Casting and Type Conversion

Difference Between Type Casting and Type Conversion

S.N. Type Casting Type Conversion
1 Type casting is a mechanism in which one data type is converted to another data type using a casting () operator by a programmer. Type conversion allows a compiler to convert one data type to another data type at the compile time of a program or code.
2 It can be used both compatible data type and incompatible data type. Type conversion is only used with compatible data types, and hence it does not require any casting operator.
3 It requires a programmer to manually casting one data into another type. It does not require any programmer intervention to convert one data type to another because the compiler automatically compiles it at the run time of a program.
4 It is used while designing a program by the programmer. It is used or take place at the compile time of a program.
5 When casting one data type to another, the destination data type must be smaller than the source data. When converting one data type to another, the destination type should be greater than the source data type.
6 It is also known as narrowing conversion because one larger data type converts to a smaller data type. It is also known as widening conversion because one smaller data type converts to a larger data type.
7 It is more reliable and efficient. It is less efficient and less reliable.
8 There is a possibility of data or information being lost in type casting. In type conversion, data is unlikely to be lost when converting from a small to a large data type.
8
float b = 3.0;
int a = (int) b
int x = 5, y = 2, c;
float q = 12.5, p;
p = q/x;






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA