Learn type casting in C++ with simple explanations and examples. Understand implicit and explicit casting, conversion between int and float, and how C++ handles data type conversion in programs.
Type conversion means converting a value from one data type to another data type.
For example:
int num = 10;
double result = num;
Here, the int value 10 is converted to double.
C++ supports two common types of conversion:
Implicit type conversion occurs automatically. The programmer does not need to write any casting instruction.
int num = 10;
double result = num;
cout << result;
Output:
10
C++ automatically converts int to double.
| From | To | Example |
|---|---|---|
char |
int |
'A' → 65 |
char |
float |
'A' → 65.0 |
char |
double |
'A' → 65.0 |
bool |
int |
true → 1 |
bool |
float |
true → 1.0 |
bool |
double |
true → 1.0 |
int |
float |
10 → 10.0 |
int |
double |
10 → 10.0 |
float |
double |
10.5 → 10.5 |
int |
char |
65 → 'A'* |
float → doublefloat → double is normally an implicit conversion.
float num = 12.5f;
double result = num;
C++ automatically performs the conversion.
char to intWhen a char is converted to an int, its ASCII value is obtained.
char ch = 'A';
int num = ch;
cout << num;
Output:
65
Because:
'A' = 65
'B' = 66
'C' = 67
int to charAn integer can also be converted to a character.
int num = 65;
char ch = num;
cout << ch;
Output:
A
Here, 65 is the ASCII value of A.
int → char can be an implicit conversion.
However, the integer should represent a meaningful character code.
For ASCII:
65–90 → A–Z
97–122 → a–z
48–57 → 0–9
For example:
int num = 65;
char ch = num;
produces:
A
But an arbitrary value such as 500 should not be expected to produce a meaningful standard ASCII character.
Explicit type conversion means that the programmer manually tells C++ to convert a value from one data type to another.
One common beginner-friendly syntax is:
(data_type)value
double num = 10.75;
int result = (int)num;
cout << result;
Output:
10
The programmer explicitly converts double to int.
Explicit conversion is especially useful when converting from a type that can contain more information to a type that can contain less information.
For example:
double num = 25.75;
int result = (int)num;
The result is:
25
The decimal part .75 is removed.
This is called loss of information.
| From | To | Example |
|---|---|---|
double |
int |
10.75 → 10 |
float |
int |
10.75 → 10 |
double |
float |
10.75 → 10.75 |
float |
char |
65.5 → 'A'* |
double |
char |
65.5 → 'A'* |
int |
char |
65 → 'A' |
int |
bool |
5 → true |
int → charAlthough int → char can happen implicitly, we can also perform it explicitly.
int num = 65;
char ch = (char)num;
cout << ch;
Output:
A
Here, (char) tells C++:
Convert this integer value to
char.
double → intdouble num = 15.89;
int result = (int)num;
cout << result;
Output:
15
The fractional part .89 is discarded.
float → intfloat num = 12.75f;
int result = (int)num;
cout << result;
Output:
12
double → floatdouble num = 25.75;
float result = (float)num;
cout << result;
Here, the programmer explicitly requests conversion from double to float.
bool ConversionA bool has two values:
true → 1
false → 0
bool → intbool status = true;
int result = status;
cout << result;
Output:
1
This can happen implicitly.
int → boolint num = 5;
bool result = num;
cout << result;
Output:
1
In a Boolean context:
0 → false
non-zero → true
| Feature | Implicit | Explicit |
|---|---|---|
| Who performs conversion? | C++ automatically | Programmer |
| Casting instruction required? | No | Yes |
| Example | double d = num; |
int n = (int)d; |
| Programmer controls conversion? | No | Yes |
| Useful for avoiding unwanted conversion? | Limited | Yes |
int num = 10;
double result = num;
C++ automatically converts:
int → double
double num = 10.75;
int result = (int)num;
The programmer manually converts:
double → int
Result:
10
char ch = 'A';
int num = ch;
Result:
65
This is an implicit conversion.
int num = 65;
char ch = num;
Result:
A
This can also be an implicit conversion because C++ allows the numeric-to-character conversion.
Explicit type casting can be used to convert a value to another data type before an arithmetic operation.
Consider the following expression:
cout << 5 / 2;
Here, both 5 and 2 are integers (int).
Therefore, C++ performs integer division:
5 / 2 = 2
We can explicitly convert 5 from int to float:
cout << (float)5 / 2;
Here:
(float)5
means:
Convert the integer value
5to afloat.
The expression is now treated as:
5.0 / 2
Since one value is a float, C++ performs floating-point division.
5.0 / 2 = 2.5
2.5
cout << 5 / 2;
cout << (float)5 / 2;
Results:
5 / 2 → 2
(float)5 / 2 → 2.5
Explicit type casting allows us to control the data type used in an expression.
In this example, (float) changes integer division into floating-point division.
int a = 5;
int b = 2;
cout << (float)a / b;
Output:
2.5
Here, only a is explicitly converted to float. Because one operand is a float, the division produces a floating-point result.
A useful pattern to remember is:
char → int → float → double
These conversions generally move toward types capable of representing a wider range or more precision.
The reverse direction can require explicit conversion and may cause information loss:
double → float → int
For example:
10.75 double
↓
10 float
↓
10 int
When converting a floating-point value to an integer, the fractional part is discarded.
float → double is normally implicit.char → int can be implicit and gives the character’s numeric code.int → char can also be implicit.65 → 'A' because 65 is the ASCII value of A.double → int can lose the decimal/fractional part.0 → false and a non-zero value → true.IMPLICIT
C++ does the conversion automatically.
Example:
int num = 10;
double d = num;
EXPLICIT
Programmer tells C++ to perform the conversion.
Example:
double d = 10.75;
int num = (int)d;