Typecasting in C/C++ | Uncover Difference between Typecasting & Type Conversion

Free C++ course with real-time projects Start Now!!

Typecasting is a salient feature of the C/C++ programming that gives the programmer the provision to convert one type of data into another type. Beginners commonly misconceive that the terms type conversions and typecasting in C/C++can be used interchangeably, it is not the case. We will discuss their key differences in order to keep our concepts crystal clear.

Typecasting in C/C++

Typecasting is a way to convert a particular data type of a variable to another data type in C/C++. It proves to be quite useful when it comes to memory management. Suppose we want to store a value of data type int into a variable of data type long, we can achieve this task by typecasting int to long. And the good news is that it is as easy as it sounds.

Syntax

int number1;
float number2;

// BODY
….
number2 = (float) number1;

Type Conversion in C and C++

Type conversion is a concept in which one type of data is automatically converted into another type without the programmer’s involvement. It is solely done by the compiler only if both the data types are compatible with each other.

Syntax

int number1 = 5;
float number2;
Number2 = a; // The value of number2 would be 5.000

Type Conversion vs Typecasting

Often people tend to use the terms typecasting and type conversions interchangeably whenever we talk about the conversion of one type of data to another, like, from int to char.

It is important to note the following key differences:

  1. Typecasting refers to the conversion of one data type to another by the user, whereas type conversion refers to the automatic conversion of one type of data to another.
  2. We generally use typecasting when both the data types are incompatible with each other. Whereas in type conversion, it is mandatory for both the data types to be compatible with each other.
  3. We require the casting operator “()” for typecasting in C/C++, whereas we do not require any such operator in the case of type conversion.
  4. We usually do type casting while writing the program, whereas we do type conversion usually during compilation.

Types of Type Conversions in C/C++

In the C/C++ programming language, type conversions are of two types, namely:

Implicit Type Conversion

It is automatic in nature as the compiler itself converts the variable from one type to the other without the employment of any other function. Hence, we do not require any operator.

It is important to understand the rules associated with implicit type conversions. They are:

  • Smaller data types are converted to larger data types to avoid the loss of data.
  • When the operation between the int and float data type is performed, the resultant value would be of floating type.

For instance, consider the following segment of code:

int x =4;
float a = 14.4, b;
b = a / x;

Now, let us understand how this works:

In implicit type conversion, the “x” in a / x is automatically converted into the bigger data type, that is, the float data type. This happens in order to make “x” equal to “a” in terms of its data type.

Therefore, the value of x = 4.0 and the expression a / x would be 14.4 / 4.0 = 3.6

  • Similarly, when the operation between char/short and int is performed, the resultant value would be of integer type as the bigger data type here is int.

Note that the value of the char data type would be treated as its ASCII value, not as its original character type when an operation is performed between the char and int data type.

For instance, consider the following segment of code:

char character = 'z';
int number = 8, sum;
sum = character + number;

Now, let us understand how it works:

We know that the ASCII value of the lowercase character ‘z’ is 122. Therefore, the compiler automatically converts the character of char data type into the integer data type and the expression sum = character + number becomes equal to 122 + 8 = 130
Therefore, the output would be 130.

From this discussion, it is pretty clear that the lower data type is converted into a higher data type.

The following diagram illustrates the ranking of a lower data type to a higher data type:

Implicit Type Conversion C and C++

Explicit Type Conversion

It is done by the programmer according to his own convenience with the help of the cast operator.

Key takeaway: The new data type should clearly be mentioned either before the identifier or the value within brackets that are to be typecasted.

Example of typecasting in C

Here is a program that illustrates the use of typecasting in C language:

#include <stdio.h>
int main ()
{

printf("Welcome to DataFlair tutorials!\n\n");

int sum = 21, count = 5;
double average;
average = (double) sum / count;
printf("The average value is : %f\n", average );
return 0;
}

 

Output-

Output of typecasting in C

Example of typecasting in C++

Here is a program in C++ that illustrates the use of typecasting:

#include <iostream>
using namespace std;

int main ()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
int sum = 21, count = 5;
double average;
average = (double) sum / count;
cout<<"The average value is : "<< average <<endl;
return 0;
}

 

Output-

Output of typecasting

Inbuilt Typecasting Functions in C/C++

There are 5 basic types of inbuilt typecast functions in C/C++:

  1. atof(): We use it to convert string data type into the float data type.
  2. atoi(): We use it to convert string data type into the int data type.
  3. atol(): We use it to convert string data type into the long data type.
  4. itoa(): We use it to convert int data type into the string data type.
  5. ltoa(): We use it to convert long data type into the string data type.

Quiz on Typecasting in C/C++

Summary

In this tutorial, we discussed the basic meaning of type conversion and typecasting in C/C++, which most people tend to mix up. We understood that it is possible to convert one type of data to another type, making it all the more flexible. Thereafter, we discussed what is implicit type conversion and explicit type conversion. We concluded our discussion by overviewing the different types of inbuilt typecasting functions in C and C++.

Feel free to ask any queries in the comment section below. We would be happy to respond.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *