Data Type Conversions

Data Type conversion is the process of converting one data type to another data type. This process is performed only on those data types in which conversion is possible. Data type conversion is performed by a compiler. In type conversion, the destination data type can’t be smaller than the source data type. Data type conversion is done at compile time and it is also called widening conversion because the destination data type can’t be smaller than the source data type.

There are two types of Data Type Conversion:

  • Explicit Data Type Conversion
  • Implicit Data Type Conversion

Explicit Data Type Conversion

Explicit data type Conversion is called type casting and it is user-defined. Here the user can typecast the result to make it of another data type.

The syntax is type (expression)

// C program to demonstrate explicit type casting #include<stdio.h>
int main() 
     double x = 1.2;
     // Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum); return 0;

Implicit Data Type Conversion

It os known as ‘automatic type conversion’.

A. This conversion is done by the compiler on its own, without any external trigger from the user.

B. Generally takes place when in an expression more than one data type is present. In such conditions type conversion done by compiler to avoid loss of data.

C. All the data types of the variables are upgraded to the data type of the variable with the largest data type.

bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double

D. It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).

Example of implicit Data type Conversion

#include <stdio.h>
 int main()
     int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97 x = x + y; // x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
No Comments

Send Comment Edit Comment

 ̄﹃ ̄
∠( ᐛ 」∠)_
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
( ๑´•ω•) "(ㆆᴗㆆ)