C++,  Programming

Type-Conversion in C++ – C++ – Lecture 9

In the previous lecture, we discussed the basics of variables in C++. In this lecture, let us learn type-conversion in C++.

Understanding Type-Conversion

In very simple words, we can convert the primitive data-types from one type to the other. This is what we call as type-conversion. For example, you can convert short to int, int to short, float to double, double to float and so on.

Observe that either the conversion would be from a lower range to a higher range(like float to double) or vice-versa(double to float). The concept of type-conversion can be broadly divided into two categories – Implicit and Explicit.

Implicit Type-Conversion in C++

Implicit conversion is the one which is automatically handled by the compiler. By default, the following rule for type-conversion is followed.

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

It means that by default, bool can be properly converted to char, char to short, short to int and so on.

Let us look at some examples.

char a = 'A';
int b = a;
cout << b;

We get 65 as the output because char has a lower range(0 to 255) than int. In this case, the compiler automatically converts the character ‘A’ to an int i.e., 65(recall ASCII table) and assigns it to the integer b. Some more examples of implicit type-conversion are:

    short a = 24;
    int b = a;
    cout << b << '\n';
    // result is an int

    int c = 23456;
    float d = 2.345;
    cout << c + d << '\n';
    // result is a float

    float e = 4.567;
    double r = 2.34455;
    cout << e + r << '\n';
    // result is a double

    double s = 2.45678;
    int t = 2;
    cout << s + t;
    // result is a double again

The output of the above code is shown below.

24
23458.3
6.91155
4.45678

During type-conversion(whether implicit or explicit), we need to take care that there is no information loss. For example:

    int a;
    double d = 4.6789;
    a = d;
    cout << a << '/n';

In this case, the precision of d is lost because the variable a is an integer, hence, the output is 4.

Explicit Type-Conversion in C++

In explicit type-conversion, we explicitly change the data-type of the value. This is known as type-casting. For example,

    float a = 3.4567;
    cout << (int)b;

What is the output of the program? 3. Why? Because we converted a float to an integer, so, there is a loss of precision(decimal places). You might argue that what is the need to use explicit type-casting? To understand explicit type-casting better, let us take one more example,

    int a = 1;
    int b = 3;
    float c = a / b; //  '/' means division
    cout << c;

What do you think the output might be?

It is 0! Why? We are dividing 2 integers, so we get an integer(floor value of 0.333, i.e., 0) and then, we assign this integer, which is 0, to c. A float can also depict an integer because it has higher range than int. That is the reason you see 0 as the output. Now, tweak this code a little bit like this,

    int a = 1;
    int b = 3;
    float c = (float)a / (float)b;
    cout << c;

This time we get the correct output(0.333333) and correct precision(6 decimal points) because now, integers a and b are converted to floating numbers, so, when we divide them, we get a float, which is stored in the variable c.

I hope this article helped you in learning about the topic you were looking for. If you found this article helpful, share it with your friends and peers who want to learn programming in a fun way!

And I will see you in the next one😀.