Type Casting in C

C programming language is best known for offering various types of functionalities and features to the programmers. C also allows the programmers to do type casting.

Typecasting and Type conversion are different things. In C, typecasting is a way to simply change the data type of a variable to another data type. Typecasting is so useful and efficient.

What is Type casting in C?

In C, When you convert the data type of a variable to another data type then this technique is known as typecasting.
Let’s say that you want to store a value of int data type into a variable of float data type.

Then you can easily do this with the help of typecasting. It is one of the important concepts of the C programming language.

Syntax for C type casting:-

int num1;
float num2;
// BODY
num2 = (float) num1; // type casting

Types of Type casting in C:-

There are 2 types of type casting operations present in C such as:-

  • Implicit Type Casting
  • Explicit Type Casting

1. Implicit Type Casting in C

It is very easy to implement implicit type casting. With the help of implicit type casting, you can convert a data type of a variable into another data type without losing its actual meaning.

It will perform the conversion without changing the importance of the values stored inside the variable.

Important Points to understand the rules of implicit type casting:-

  • If you are doing a conversion on two different data types then the lower data type is automatically converted into a higher data type.
  • Suppose, you are doing type casting operation between two data types such as int and float then the value of the result would be floating type.

Example1:- Implicit Type Casting

int a =4;
float x = 12.4, y;
y = x / a;

In the above example, the variable ‘a’ will be automatically converted to float data type which is a bigger data type. And the variable ‘a’ will be equal to ‘x’ in terms of its data type. So, the value of y will be 12.4/4.0=3.1.

Converting Character to Int:-

With the help of type casting, you can convert character data type to an int data type. If you are performing conversion between int and character data types then the value of the result will be integer type. Because it is a bigger data type between int and char data types.

In this conversion, the value of char data type is converted into its ASCII value and then processed further.

Example:- Char to Int:-

#include <stdio.h>
int main() {
   int num = 10;
   char c = 'T';
   int sum;
   sum = num + c;
   printf("TechVidvan Tutorial: Converting char to int!\n");
   printf("Value of sum : %d\n", sum);
   return 0;
}

Output:-

TechVidvan Tutorial: Converting char to int!
Value of sum : 94

In the above example, the compiler converted the value of char c variable to its ASCII value. So, the ASCII value of the character ‘T’ is 84. Then it is added to the number 10. At last, it will print 94 as the output.

Arithmetic Conversion Hierarchy:-

The compiler will first perform integer promotion. Then it will check if the two operands have different data types or not. If they have different data types then they will be converted according to the highest in the following hierarchy:-

Arithmetic Conversion Hierarchy in C

Example:- Arithmetic Conversion Hierarchy

#include <stdio.h>
int main() {
   int  num = 21;
   char ch = 'T';
   float sum;
   sum = num + ch;
   printf("TechVidvan Tutorial: Arithmetic Conversion Hierarchy!\n");
   printf("Value of sum is: %f\n", sum);
}

Output:-

TechVidvan Tutorial: Arithmetic Conversion Hierarchy!
Value of sum is: 105.000000

In the above example, the ‘ch’ variable is converted into its ASCII value, but the compiler converts num and ch into float data type. And after adding is done, it will produce a float data type value as the output.

2. Explicit Type casting in C

It is not like implicit type casting where the data type is converted automatically. In Explicit type casting, you have to force the conversion between data types.

Syntax:-

(data_type_name) expression
  • data_type_name is the name of the data type to which you want to convert to.
  • The expression can be a variable, an actual expression or a constant.

Example:- Explicit Type Casting

#include<stdio.h>
int main()
{
    float val = 56.3;
    int a = (int)val + 50; // explicit type casting
    printf("TechVidvan Tutorial: Explicit Type Casting!\n");
    printf("Value of val: %f\n", val);
    printf("Value of a: %d\n",a);
    return 0;
}

Output:-

TechVidvan Tutorial: Explicit Type Casting!
Value of val: 56.299999
Value of a: 106

In the above example, we declared a float type variable named val. Then the float type variable is converted to int data type through explicit type casting. And then it is being added. So, you will get an integer as output.

Integer Promotion:-

With the help of integer promotion, you can convert smaller integer type values than int or unsigned int to either int or unsigned int.

Example:- Integer Promotion

#include <stdio.h>
int main(){
   int num= 21;
   char c = 'T';
   int sum;
   printf("TechVidvan Tutorial: Integer Promotion!\n");
   sum = num + c;
   printf("The sum is: %d\n", sum);
   return 0;
}

Output:-

TechVidvan Tutorial: Integer Promotion!
The sum is: 105

In the above example, the value of sum is 105. The compiler is doing an integer promotion and it is converting the value of ‘c’ to ASCII then adding it to the value of num variable.

In-built type casting functions in C:-

In C, there are 5 different type casting functions available.

  • atof(): Used for converting the string data type into float data type.
  • atoi(): Used for converting the string data type into int data type.
  • atbol(): Used for converting the string data type into long data type.
  • itoba(): Used for converting the int data type into string data type.
  • ltoa(): Used for converting the long data type into string data type.

Differences between typecasting and type conversion:-

People often get confused when they hear about typecasting and type conversion. They think that both are the same. But they are not. Look at the following differences:-

  • If one type of data converts into another data type without any human interaction then it is known as type conversion. But if a user converts the type of data to another data type then it is known as type casting.
  • In typecasting, you have to use the operator ‘()’ but in type conversion, you don’t have to use any operator.
  • Type casting is done while writing the program. But type conversion is done during compilation.
  • You use type casting when both the data types are not compatible with each other. But it is mandatory to both the data types to be compatible with each other.

Advantages of Type Casting in C

  • It helps you in converting one data type to another data type.
  • It helps in making the program lightweight.
  • With this, you can take advantage of features like type representations and hierarchies.

Summary

In this tutorial, we learnt about type casting in C. We discussed the types of type casting present in C. We also learnt the arithmetic conversion hierarchy. Then we also talked about the in-built type casting functions in C.

Finally, we also explored the differences between type casting and type conversion and why people often get confused over it. With type casting, you can simply convert a type of data into another data type.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.