When a programmer or user converts a data type to another within a program's code, it is referred to as type casting. This technique is applied manually to transform one data type into another, especially when changing the target data type. It is important to note that the destination data type should be smaller than the source data type, leading to its classification as a narrowing conversion.
Syntax:
Destination_datatype = (target_datatype) variable;
(data_type) it is known as casting operator
Targetdatatype: This refers to the specific data type to which we aim to convert the destination data type. The variable represents a particular value that needs to be transformed into the targetdata type. Let's explore the idea of type casting through an illustration.
Suppose we aim to transform the float data type into an int data type. In this scenario, the int data type is smaller than the float data type due to int being 2 bytes while float is 4 bytes. During this conversion, the float variable's value gets truncated and becomes an integer variable. Casting is applicable with both compatible and non-compatible data types.
float b = 3.0;
int a = (int) b; // converting a float value into integer
Let's explore type casting using a C program.
AreaOfRectangle.c
#include<stdio.h>
#include<conio.h>
void main()
{
printf("\n Welcome to Logic Practice tutorials ");
float x = 3.5, y = 4.5; // the size of float variable is 4 byte.
int area; // the size of the int variable is 2 bytes.
area = (int) x * y; // after conversion the product converts into integer
printf("\n Area of a Rectangle is : %d", area);
printf("\n Here, we convert float data type into the Int data type");
getch();
}
Output:
[Program Output]
What is type conversion?
When a data type undergoes automatic transformation into another data type during compilation, it is referred to as type conversion. This process is executed by the compiler when the two data types are compatible. It is essential to note that the target data type must not have a smaller size than the source type. This operation is also recognized as widening conversion of the data type.
Let's explore type conversion through an illustration.
Suppose we have an integer data type that needs to be converted to a floating-point data type. These data types are compatible as both are numeric, with the integer type being 2 bytes in size, which is smaller than the float data type. Consequently, the compiler performs an automatic conversion between the data types without any loss or truncation of values.
int a = 20;
Float b;
b = a; // Now the value of variable b is 20.000 /* It defines the conversion of int data type to float data type without losing the information. */
In the previous example, the integer data type is transformed into a floating-point data type, which possesses a greater size than an integer, resulting in the widening of the source data type.
Let's explore type conversion using a C program.
#include<stdio.h>
#include<conio.h>
void main()
{
printf("\n Welcome to Logic Practice tutorials ");
int x = 3, y = 4; // the size of int variable is 2 byte.
float area; // the size of float variable is 4 bytes.
area = x * y; /* It is a type conversion that automatically converted by the compiler at the compile time of a program. */
printf("\n Area of a Rectangle is : %f", area);
printf("\n Here, we convert int data type to the float data type");
getch();
}
Output:
[Program Output]
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. |
int a = (int) b | int x = 5, y = 2, c;
float q = 12.5, p;
p = q/x; |
float b = 3.0;
int a = (int) b
int x = 5, y = 2, c;
float q = 12.5, p;
p = q/x;