Type Casting and Type Coversion
Type Casting and Type Coversion
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.
AreaOfRectangle.c
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. printf("\n Welcome to Javatpoint tutorials ");
6. float x = 3.5, y = 4.5; // the size of float variable is 4 byte.
7. int area; // the size of the int variable is 2 bytes.
8. area = (int) x * y; // after conversion the product converts into integer
9. printf("\n Area of a Rectangle is : %d", area);
10. printf("\n Here, we convert float data type into the Int data type");
11. getch();
12. }
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.
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.
1. int a = 20;
2. Float b;
3. b = a; // Now the value of variable b is 20.000 /* It defines the conversio
n of int data type to float data type without losing the information. */
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.
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. printf("\n Welcome to Javatpoint tutorials ");
6. int x = 3, y = 4; // the size of int variable is 2 byte.
7. float area; // the size of float variable is 4 bytes.
8. area = x * y; /* It is a type conversion that automatically converted by the
compiler at the compile time of a program. */
9. printf("\n Area of a Rectangle is : %f", area);
10. printf("\n Here, we convert int data type to the float data type");
11. getch();
12. }
1 Type casting is a mechanism in which Type conversion allows a compiler to convert one
one data type is converted to another data type to another data type at the compile time of
data type using a casting () operator by a program or code.
a programmer.
2 It can be used both compatible data Type conversion is only used with compatible data
type and incompatible data type. types, and hence it does not require any casting
operator.
4 It is used while designing a program It is used or take place at the compile time of a
by the programmer. program.
5 When casting one data type to another, When converting one data type to another, the
the destination data type must be destination type should be greater than the source
smaller than the source data. data type.