Type Casting in C Language with Program Example

The C type casting is renowned for providing programmers with a wide range of capabilities and functionalities.

Type conversion and type casting are distinct concepts.

Typecasting in C is a technique for quickly switching a variable’s data type to another data type. Typecasting is incredibly effective and beneficial.

Type conversion, also known as type casting, is the transformation of one datatype into another.

For instance, you can type convert a “long” value to a “int” if you want to store it as a straightforward integer.

The cast operator can be used to explicitly transform values from one type to another as shown in the following:

(type_age) expression

What is Type casting in C?

Typecasting in C is the term used in C to describe the process of changing a variable’s data type.

Let’s imagine you wish to assign an int data type value to a float data type variable.

Then typecasting can let you accomplish this task with ease. One of the key ideas in the C programming language is this.

Syntax:

int number1;
float number2;
// BODY
number2 = (float) number1; // type casting

Take a look at the example below, where the cast operator divides one integer variable by another into a floating-point operation.

#include <stdio.h>

int main()

int addition = 17, count = 5;
double mean;

mean = (double) addition / count;
printf("Value of mean : %f\n", mean );
}

Output of the above example:

Value of mean : 1.301887

The cast operator takes precedence over division in this situation, thus the value of the sum is first transformed to type double and then divided by count, producing a double value.

You can test the above example here! ➡ C Online Compiler

Types of Type casting in C:

In C, there are two different sorts of type casting processes, including:

  • Implicit Type Casting
  • Explicit Type Casting

Implicit Type Casting C

Implicit type casting in C is fairly simple to use. Implicit type casting allows you to change a variable’s data type into another data type without altering the meaning of the original data type.

Without altering the significance of the values kept inside the variable, it will conduct the conversion.

Important Points to Remember When Understanding Implicit Type Casting Rules:

  • The lower data type is automatically translated into a higher data type when you perform a conversion on two different data types.
  • If you were to do a type casting operation between two data types, such as an int and a float, the result would have a floating type value.

Example 1 of Implicit Type Casting C

int l = 4;
float m = 12.4, n;
n = m / l;

The variable “l” in the aforementioned example will automatically be changed to the larger data type float.

And the data type of the variable “l” will be the same as that of “m.” As a result, y will have the value 12.4/4.0=3.1.

Explicit C-type casting

It differs from implicit type casting, which converts the data type for you.

You must force the conversion between data types via explicit type casting.

Syntax:

(data_type_name) expression

  • The name of the data type you want to convert into is data type name.
  • The expression could be a constant, a real expression, or a variable.

Example of Explicit Type casting C

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

Output of the above example:

C Programming Tutorial: Explicit Type Casting!
Value of val: 47.299999
Value of a: 90

We declared a float-type variable called X in the example above. The variable’s float data type is then explicitly type cast to an int data type.

After that, it is added. As a result, the output will be an integer.

You can test the above example here! ➡ C Online Compiler

In-built type casting functions in C

Five different type casting functions are available in C.

  • Use the atof() function to change a string data type to a float data type.
  • Use the atoi() function to change a string data type to an int data type.
  • atbol(): This function is used to change a string data type to a long data type.
  • Use the itoba() function to change an int data type to a string data type.
  • Use ltoa() to change a long data type into a string data type.

Type conversion and type casting’s differences

When people learn about typecasting and type conversion, they frequently become perplexed.

They believe both to be the same. They are not, though. Take a look at the following variations:

  • Type conversion is the process by which one type of data transforms into another type of data without the involvement of a human. However, type casting refers to the process of converting one data type into another.
  • Unlike type conversion, where no operator is required, typecasting requires the usage of the operator “().”
  • The software is type cast as it is being written. Type conversion, however, happens during compilation.
  • When the two data types are incompatible with one another, type casting is used. However, compatibility between the two data formats is a need.

Advantages of Type Casting C

  • It aids in the conversion of one data type to another.
  • It contributes to the program’s lightness.
  • Utilizing this, you can benefit from attributes like type representations and hierarchies.

Summary

In this tutorial, typecasting in C was covered. We spoke about the many methods of type casting C. The hierarchy of mathematical conversions was also taught to us.

Finally, we looked at the distinctions between type conversion and type casting and the reasons why people frequently conflate the two. You may easily change one type of data into another using type casting.

About Next Tutorial

In the next post, I’ll create an Error Handling in C and try to explain its many components in full detail.

I hope you enjoy this post on How Many Types of Casting in C Language, in which I attempt to illustrate the language’s core grammar.


Leave a Comment