Difference between Type Casting and Type Conversion
Last Updated :
07 Mar, 2025
1. Type Casting: In type casting, a data type is converted into another data type by the programmer using the casting operator during the program design. In type casting, the destination data type may be smaller than the source data type when converting the data type to another data type, that's why it is also called narrowing conversion.
Syntax/Declaration:-
destination_datatype = (target_datatype)variable;
(): is a casting operator.
target_datatype: is a data type in which we want to convert the source data type.
Type Casting example -
float x;
byte y;
...
...
y=(byte)x; //Line 5
In Line 5: you can see that, we are converting float(source) data type into byte(target) data type.
2. Type conversion : In type conversion, a data type is automatically converted into another data type by a compiler at the compiler time. In type conversion, the destination data type cannot be smaller than the source data type, that's why it is also called widening conversion. One more important thing is that it can only be applied to compatible data types.
Type Conversion example -
int x=30;
float y;
y=x; // y==30.000000.
Let's see the difference between Type casting and Type conversion which are given below:
S.NO | TYPE CASTING | TYPE CONVERSION |
---|
1. | In type casting, a data type is converted into another data type by a programmer using casting operator. | Whereas in type conversion, a data type is converted into another data type by a compiler. |
2. | Type casting can be applied to compatible data types as well as incompatible data types. | Whereas type conversion can only be applied to compatible datatypes. |
3. | In type casting, casting operator is needed in order to cast a data type to another data type. | Whereas in type conversion, there is no need for a casting operator. |
4. | In type casting, the destination data type may be smaller than the source data type, when converting the data type to another data type. | Whereas in type conversion, the destination data type can't be smaller than source data type. |
5. | Type casting takes place during the program design by programmer. | Whereas type conversion is done at the compile time. |
6. | Type casting is also called narrowing conversion because in this, the destination data type may be smaller than the source data type. | Whereas type conversion is also called widening conversion because in this, the destination data type can not be smaller than the source data type. |
7. | Type casting is often used in coding and competitive programming works. | Whereas type conversion is less used in coding and competitive programming as it might cause incorrect answer. |
8. | Type casting is more efficient and reliable. | Whereas type conversion is less efficient and less reliable. |
Similar Reads
Difference between fundamental data types and derived data types In computer programming, data type is a classification that specifies to compiler or interpreter which type of data user is intending to use. There are two types of data types - Primitive/Fundamental data type: Each variable in C/C++ has an associated data type. Each data type requires different amo
8 min read
Difference between Boxing and Unboxing in C# Boxing and unboxing is an important concept in C#. C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, it converts a Value Type to a Reference Type, and vice versa. Boxing and Unboxing enables a unified view of the type syste
2 min read
Catch block and type conversion in C++ Predict the output of following C++ program. C++ #include <iostream> using namespace std; int main() { try { throw 'x'; } catch(int x) { cout << " Caught int " << x; } catch(...) { cout << "Default catch block"; } } Output: Default catch block In the above
2 min read
Type Conversion in C In C, type conversion refers to the process of converting one data type to another. It can be done automatically by the compiler or manually by the programmer. The type conversion is only performed to those data types where conversion is possible. Let's take a look at an example:C#include <stdio.
4 min read
Difference between an Integer and int in Java with Examples In Java, int is a primitive data type while Integer is a Wrapper class.int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipul
6 min read