0% found this document useful (0 votes)
35 views4 pages

Type Casting and Type Coversion

The document discusses type casting and type conversion in programming languages. Type casting is when a programmer explicitly converts a data type to another type, which may result in data loss. Type conversion is when the compiler automatically converts between compatible types during compilation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Type Casting and Type Coversion

The document discusses type casting and type conversion in programming languages. Type casting is when a programmer explicitly converts a data type to another type, which may result in data loss. Type conversion is when the compiler automatically converts between compatible types during compilation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

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.

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.

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.

Let's understand type conversion through a C program.

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. }

Difference Between Type Casting and Type Conversion

S.N. Type Casting Type Conversion

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.

3 It requires a programmer to manually It does not require any programmer intervention to


casting one data into another type. 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 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.

6 It is also known as narrowing It is also known as widening conversion because one


conversion because one larger data smaller data type converts to a larger data type.
type converts to a smaller data type.

7 It is more reliable and efficient. It is less efficient and less reliable.


8 There is a possibility of data or In type conversion, data is unlikely to be lost when
information being lost in type casting. converting from a small to a large data type.

8 float b = 3.0; int x = 5, y = 2, c;


int a = (int) b float q = 12.5, p;
p = q/x;

You might also like