bscodinglab

Type Casting

Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a ‘long’ value into a simple integer then you can type cast ‘long’ to ‘int’. You can convert the values from one type to another explicitly using the cast operator .

As follows :−          (type_name) expression

Example :-                int x;

                                    float y;

                                    y = (float) x;

Types of Type Casting in C :- Thare are two types of type-casting.

  • Implicit type casting
  • Explicit type casting
  1. Implicit Type Casting :-  Implicit type casting means conversion of data types without losing its original meaning. This type of typecasting is essential when you want to change data types without changing the significance of the values stored inside the variable.

Implicit type conversion in C happens automatically when a value is copied to its compatible data type. During conversion, strict rules for type conversion are applied. If the operands are of two different data types, then an operand having lower data type is automatically converted into a higher data type. This type of type conversion can be seen in the following example.

Example :- 

#include <stdio.h>

      main()

      {

            int  number = 1;

            char character = ‘k’; /*ASCII value is 107 */

            int sum;

            sum = number + character;

            printf(“Value of sum : %d\n”, sum );

      }

Output :-  The result is 7.000000

 

We cannot perform implicit type casting on the data types which are not compatible with each other such as:

  • Converting float to an int will truncate the fraction part hence losing the meaning of the value.
  • Converting double to float will round up the digits.
  • Converting long int to int will cause dropping of excess high order bits.

 

  1. Explicit Type Casting :-

In implicit type conversion, the data type is converted automatically. There are some scenarios in which we may have to force type conversion. Suppose we have a variable div that stores the division of two operands which are declared as an int data type.

Example :- 

                                    #include <stdio.h> 

                        // Driver Code

                        int main()

                        {

                                // Given a & b

                              int a = 15, b = 2;

                              float div;

                              // Division of a and b

                              div = a / b;

                              printf(“The result is %f\n”, div);

                              return 0;

                        }

Output :-  The result is 7.000000

 

 

In C programming, there are 5 built-in type casting functions.

  • atof(): This function is used for converting the string data type into a float data type.
  • atbol(): This function is used for converting the string data type into a long data type.
  • Itoa(): This function is used to convert the long data type into the string data type.
  • itoba(): This function is used to convert an int data type into a string data type.
  • atoi(): This data type is used to convert the string data type into an int data type.

Advantages of Type Casting :-

  • Type casting in C programming makes the program very lightweight.
  • Type representation and hierarchies are some features we can take advantage of with the help of typecasting.
  • Type casting helps programmers to convert one data type to another data type.