Type casting

We can convert the data type of a value to another data type using type casting.

Let us understand several conversions with the help of a few examples!

The float to int conversion

Look at the following example:

Press + to interact
#include<iostream>
using namespace std;
//example 1 of type casting (implicit type-casting)
int main()
{
float a = 50.88;
cout << "Before type casting: " << a << endl;
int b = a;
cout << "After type casting: " << b << endl;
//another variant of example 1 of type casting (explicit type-casting)
cout << "After type casting (alternative): ";
// we can also write the above as (int)a
cout << int(a) << endl;
cout << "The value in a is: "<< a << endl;
return 0;
}
  • In line 7, there is a variable of a float data type, a, that is assigned a value of 50.88.

  • In line 10, the value of a is assigned to integer b. The point to note is that when we assign a float value to an integer variable, it stores only the integer part, which is 50, and skips the decimal part in typecasting.

  • Lines 10 and 17 mean the same thing. We can use either way for type-casting.

There are two ways to perform type casting:

  • Implicit type casting: This is done automatically by the compiler and may result in the loss of some information (such as a truncated decimal part in the example above).
  • Explicit type casting: This is explicitly done by the programmer to convert one data type to another. For more details, read this.

The char to int conversion

Look at the following example:

Press + to interact
#include<iostream>
using namespace std;
// example 2 of type casting
int main()
{
cout << "Before casting: ";
char symbol = 'A';
cout << symbol << endl;
cout << "After casting: ";
// we can also write it like (int)symbol
cout << int(symbol) << endl;
return 0;
}

So, in the above example, we are casting a character to an integer in line 15 just like in the first example. When we convert char to int, it replaces the ASCII value of the character.

The above program can also be written as follows:

Press + to interact
#include<iostream>
using namespace std;
//another variant of example 2 of type casting
int main()
{
cout << "Before casting: ";
char symbol = 'A';
cout << symbol << endl;
cout << "After casting: ";
int a = symbol;
cout << a << endl;
return 0;
}

The above example is another variant of the previous example. Here, we make another integer variable and store the ASCII value of the character symbol in that variable.

Practice Exercise

Solve the following problems.

Problem 1: ASCII values (capital letters)

Change the above program and find the ASCII value of 'B', 'C', and 'D'.

Problem 2: ASCII values (capital letters)

Without changing the above program, can you guess the ASCII value of 'Y' and 'Z'? Verify your guess.

Problem 3: ASCII values (small letters)

Change the above program and find the ASCII value of 'a', 'b' and 'c', and guess the value of 'z' and verify it.

Problem 4: ASCII values (capital to small letters conversion)

What will be the output of the following program?

Press + to interact
#include<iostream>
using namespace std;
//another variant of example 2 of type casting
int main()
{
cout << "Before casting: ";
char symbol = 'A';
symbol += 32;
cout << symbol<< endl;
return 0;
}

We can see that the difference between the ASCII values of a (ASCII value 97) and A (ASCII value 65) is 32, which is why we add or subtract 32 when converting.

Problem 5: ASCII values (capital to small letters conversion)

In the above program, change the value of the symbol to 'B'. What will be the output of the program?

Problem 6: ASCII values (small to capital letters conversion)

Write the following program, which converts the small letter to capital.

Press + to interact
#include<iostream>
using namespace std;
//another variant of example 2 of type casting
int main()
{
cout << "Before casting: ";
char symbol = 'a';
// write your code here
// --------------------
cout << symbol << endl; // this should print capital letter A
return 0;
}