Updated March 17, 2023
Overview of Type Casting
Typecast is a way of changing an object from one data type to the next. It is used in computer programming to ensure a function handles the variables correctly. A typecast example is the transformation of an integer into a string. This could be used to compare two numbers if one is stored as a string and the other is an integer. If compatible, Java must automatically perform a conversion called Automatic Type Conversion and, if not, it must be specifically cast or converted.
What is Type Casting?
Typecasting may be defined as the process of converting the data type of the outcome of any operation to another data type. This is one of the C language’s crucial options to protect the unwanted consumption of memory. It is the way to make the variable store the value of any operation in a manner so that it consumes the limited memory. It is actually introduced to improve the efficiency of memory management. Typecasting makes it very simple to convert the values’ data type, but we have to make sure that the values that we are converting should be the right ones. For instance, converting the character to integer may sound strange but converting the float value into integer makes sense.
Typecasting can be introduced using the appropriate syntax, which has a particular way of defining. Sometimes the type conversion may happen on its own, while sometimes, we will need to do that. We will be covering the mode of conversion in the next section. We have to take several things while using type casting, like the correct syntax, the correct data type that has to be converted, and so on. It helps develop the program that works smoothly due to very little memory consumption and helps the program in its fast execution.
Types of Type Casting
Based on how the conversion of the value of any data type is taking place, the typecasting has been divided into two types that are explained below:
1. Implicit Conversion
In this mode of typecasting, the value of one data type could be converted to the other on its own, and we will not be required to mention anything. It all happens on its own or automatically. It usually happens when the variable that has to store the converted value has the size more as compared to the value that has to be converted. For instance, when there is a need to convert the integer value to the float value, it will happen as the size of the integer is 2 bytes while the float can hold a value of 4 bytes.
Example:
Float a = 7/6
2. Explicit Conversion
In this kind of typecasting, we are supposed to explicitly define the data type in which we want to convert any value. unlike implicit conversion, it has to be mentioned the data type before the operation or value in which it has to be converted. It is used when we have to store the value of any data type that occupies more memory as compared to the variable that is going to store it. For example, the float variable occupies 4 bytes while the integer takes 2 bytes of the memory. In order to store the float value in the integer variable, we will need to mention it before the float value that we want to convert it into an integer. as an outcome, the values existing after a decimal of the float value will vanish, and whatever was there before the decimal will be stored in the integer variable.
Example:
Int a = (int) 7/6
Understanding Type Casting with an Example
Here we will walk through a simple example to understand how typecasting works in the real program. So let’s get started!
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float i=3.54;
int p;
p = (int) i;
printf("Explicit value is %d",p);
getch();
}
Output:
3
In the above example, we have taken a float variable that will be storing the value 3.54. Being a float variable, it is consuming 4 bytes of memory. Our goal here was to store the float value into the integer variable by using explicit typecasting. In the above example, it is the 7th line that introduces the concept of type casting into the program. The int keyword written right before the I variable has converted the float value of I into an integer value that will eventually lose all the values after the decimal and will only retain the value before the decimal. The outcome of the above code will be 3, an integer value and converted from the float data type.
Conclusion
Typecasting can be considered as the functionality that lets us convert the data type of any value or variable so that it could be stored in a manner that consumes less memory. It is available in the C language and helps us make the program light weighted, which directly leads to the program’s fast execution. There are several data type conversion that could be done using this feature. It can be perfectly used in any kind of program regardless of its size and complexity.
The program developed with keeping memory management as the primary goal must have to use this option as it the only feature in C language that ensures the type conversion in runtime. In addition to making the program execution a bit fast, it also makes the program look sophisticated and helps other programs understand what’s going on with ease. It doesn’t need any expertise to work with typecasting, and all one needs to make sure that data type has to be considered while conversion.
Recommended Articles
This is a guide to What is Type of Casting. Here we discuss the types, understanding, and examples of Type Casting along with code. You may also look at the following articles to learn more-