0% found this document useful (0 votes)
1K views5 pages

Type Casting and Type Conversion

Type casting and type conversion both refer to changing one data type to another, but they differ in important ways: - Type casting involves explicitly changing the data type using a cast operator, while type conversion is done implicitly by the compiler between compatible types. - Type casting can change between compatible or incompatible types, but type conversion only works for compatible types. - Type casting involves narrowing conversion where data may be lost, while type conversion uses widening conversion and preserves data. - Key differences are that type casting is done manually by the programmer and allows for potential data loss, while type conversion is automatic and preserves data when changing between compatible types.
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)
1K views5 pages

Type Casting and Type Conversion

Type casting and type conversion both refer to changing one data type to another, but they differ in important ways: - Type casting involves explicitly changing the data type using a cast operator, while type conversion is done implicitly by the compiler between compatible types. - Type casting can change between compatible or incompatible types, but type conversion only works for compatible types. - Type casting involves narrowing conversion where data may be lost, while type conversion uses widening conversion and preserves data. - Key differences are that type casting is done manually by the programmer and allows for potential data loss, while type conversion is automatic and preserves data when changing between compatible types.
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/ 5

Type Casting and Type Conversion

The 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:

1. Destination_datatype = (target_datatype) variable;


2. (data_type) it is known as casting operator

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.

1. float b = 3.0;
2. int a = (int) b; // converting a float value into integer

Let's understand the type casting through a C program.

AreaOfRectangle.c

#include<stdio.h>
#include<conio.h>
void main()
{
printf("\n Welcome to Javatpoint 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:

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 conversion of i
nt 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.

#include<stdio.h>
#include<conio.h>
void main()
{
printf("\n Welcome to Javatpoint 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 com
piler 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:

Difference Between Type Casting and Type Conversion

S.N. Type Casting Type Conversion

1 Type casting is a mechanism in Type conversion allows a compiler to


which one data type is converted to convert one data type to another data type
another data type using a casting () at the compile time of a program or code.
operator by a programmer.

2 It can be used both compatible data Type conversion is only used with
type and incompatible data type. compatible data types, and hence it does
not require any casting operator.

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


manually casting one data into intervention to convert one data type to
another type. 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
by the programmer. of a program.

5 When casting one data type to When converting one data type to another,
another, the destination data type the destination type should be greater than
must be smaller than the source the source data type.
data.

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


conversion because one larger data because one smaller data type converts to
type converts to a smaller data type. 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 In type conversion, data is unlikely to be
information being lost in type lost when converting from a small to a
casting. 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