Difference Between Type Casting and Type ConversionThe 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. 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: 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. 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
|