C++

What is Typecasting in C++?

Typecasting refers to a method of converting one data type to another for the sake of computational ease. The C++ programming language also supports typecasting. As far as this article is concerned, we will take an in-depth look at what this phenomenon is and how it can be implemented in C++ on a Ubuntu 20.04 system.

What is Typecasting in C++?

We have already stated that typecasting converts a variable or an expression from one data type to another. There are mainly two types of typecasting in C++, i.e., implicit typecasting and explicit typecasting. In the former type, we do not specify the data type in which we want to typecast the expression, whereas, in the latter type, we explicitly state the data type in which we want to convert the given expression.

Examples of Typecasting in C++ in Ubuntu 20.04:

The following examples have been designed to teach you some of the different forms of typecasting by relating them with relevant scenarios. After going through all these examples, you will be able to know how you can convert one data type to another using the typecasting technique in C++ effectively.

Example # 1: Conversion of a Number to its Equivalent ASCII Character through C Style Typecasting:

In this example, we wanted to pass a number to our C++ code and convert it to its equivalent ASCII character using C style typecasting. The typecasting data type is enclosed within round brackets in the C programming language, followed by the expression to be type casted. You will be able to get this typecasting style by going through the following C++ program:

For this particular example, we have created a file named “TypeCasting.cpp” that will contain our C++ code. We have included the required library first in this code, followed by the “std” namespace. Then, we have our “main()” function in which we have simply used the “cout” statement that will print the ASCII equivalent of the number “65” on the terminal.

Once we have saved our C++ code, we have compiled it using the command shown below:

$ g++ TypeCasting.cpp –o TypeCasting

We have used the “g++” compiler for compiling our C++ code, “TypeCasting.cpp” is our source file, whereas “TypeCasting” will be the object file that will be created as a result of this compilation.

Now, we can execute our object file with the following command:

$ ./TypeCasting

The ASCII equivalent character of the number “65” is “A” as shown in the image below:

Example # 2: Generating the Entire ASCII Table through C Style Typecasting:

We can also generate the entire ASCII table using the very same C style typecasting in Ubuntu 20.04. For that, we have implemented the following C++ code on our Ubuntu 20.04 system:

In this C++ code, after including the necessary library and namespace, we have our “main()” function in which we have a “for” loop. This loop iterates over a variable named “alphabet”. We have initialized this variable with the value “0” whereas the terminating condition for this loop is “alphabet<128”. After that, we have simply incremented our “alphabet” variable. Within the body of this loop, we have our “cout” statement that will print the ASCII equivalent character corresponding to each alphabet from 0 to 127.

After saving this C++ code, we have compiled and executed it as a result of which we were able to generate the entire ASCII table on our Ubuntu 20.04 terminal as shown in the image below:

Example # 3: Conversion of a Float to an Integer for an Assignment Operation through Functional Typecasting:

In this example, we will learn a different typecasting method known as functional typecasting. In this method of typecasting, we have the data type without any brackets followed by the expression to be type casted written inside round brackets. This way of typecasting looks more like calling a function in C++ which is why it is known as functional typecasting. Moreover, this is a form of explicit typecasting. Our main goal in this example is to convert a floating-point number to an integer for an assignment operation. You can see the following C++ code to know exactly what we intend to do:

In this C++ code, we have declared a float variable “x” and assigned it the value “12.4”. Then, we have declared a variable “y” which has the integer data type. We wanted to assign the value of variable “x” to “y” which is possible only if “x” is also an integer. Therefore, we have to typecast the variable “x” into integer data type while assigning it to “y”. Finally, we wanted to print the value of the variable “y” on the terminal to see if the variable assignment had taken place correctly or not.

When we executed this code, the value of the variable “y” turned out to be “12” which means that the variable assignment was successful because whenever we try to typecast a floating-point number to an integer, its decimal part is always truncated. This can be seen from the image below:

Example # 4: Conversion of an Integer to a Float for an Assignment Operation through Implicit Typecasting:

In this example, we will learn yet another different typecasting method that is known as implicit typecasting. In this method of typecasting, we do not explicitly specify the data type in which we want to typecast our variables; rather, this decision is taken at the runtime according to the variable’s data type to which a value is being assigned. Our main goal in this example is to convert the division result of two integers to a floating-point number for an assignment operation. You can see the following C++ code to know exactly what we intend to do:

In this C++ code, we have declared two integer variables, “x” and “y” and assigned them the values “12” and “5” respectively. Then, we have declared a variable “z” which has the float data type. We wanted to assign the result of “x/y” to “z” which is possible only if the result of “x/y” is also a float. However, in the case of implicit typecasting, we do not necessarily need to convert “x/y” into a float; rather, it can be assigned as it is to the variable “z” as we did in our code. Finally, we wanted to print the value of the variable “z” on the terminal to see if the variable assignment had taken place correctly or not.

When we executed this code, the value of the variable “z” turned out to be “2” which means that the variable assignment was successful concerning implicit typecasting because whenever we try to typecast an integer number to a float with implicit typecasting, its decimal part is always truncated. This can be seen from the image below:

Conclusion:

We wanted to explain to you the concept of typecasting in C++ in Ubuntu 20.04 in this article. We first explained the different types of typecasting, after which we stated a few different examples which elaborated the concept of typecasting in C++. This article just gave a basic overview of typecasting in C++. Following the same lines, you can also perform the conversion of other data types.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.