Skip to content

Type casting in C

What is type casting?

Type casting is the process of converting a variable from one data type to another. If it makes sense, the compiler will transform one type of data into another. For example, when a float type variable is given an integer type value, then the compiler automatically converts it to float. Type casting allows you to make this type conversion explicit or force it when it would not otherwise occur.

Type Casting In C Language

Type casting in C can be classified into two types:-

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion

Implicit type conversion or type promotion refers to the type of conversion that is executed automatically by the compiler without the assistance of the programmer.

Example

Features of Implicit Type Conversion

The features of Implicit Type Conversion are listed below:-

  1. It is done by the compiler on its own, without any external trigger from the user.
  2. It generally takes place when in an expression, more than one data type is present. In such cases, type conversion helps avoid the loss of data.
  3. All the data types of the variables are upgraded to the data typeof the variable with the largest data type.
  4. It is also called standard type conversion.
  5. Conversion of smaller data types to larger data types is called Type promotion.
  6. This type of conversion is feasible only in the case of compatible data types.
  7. No keywords or special statements are needed in this type of conversion.

Bool -> char -> short int -> int -> unsigned int -> long -> unsigned ->

long long -> float -> double -> long double

 

  1. Implicit conversions can lose data, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).

Integer Promotion

The process of converting values of integer types “smaller” than int or unsigned int to int or unsigned int is known as integer promotion.

Example

#include <stdio.h>

main() {

int i = 17;

char c = ‘c’; /* ascii value is 99 */

int sum;

sum = i + c;

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

}

Output

Value of sum : 116

Arithmetic Conversion Hierarchy

It is the order which is followed by the type cast functionality. The compiler will start by promoting integers. It will then determine whether the two operands have distinct data types. If they have different data types, the highest in the following hierarchy will be used to convert them:-

Explicit Type Conversion

Explicit type conversion is the type of conversion performed by a programmer by posing the data type of an expression of a specified type. Type casting is another term for explicit type conversion.

Syntax

(data_type) expression;

,where data_type is any valid data type used in C

and expression is any constant or variable expression in C

Example

If we want to get the exact value after division, then we will have to explicitly cast the data type from int to to float. This can be done in the following manner:

Rules for type casting

The following rules must be followed while type casting from one data type to another:-

  1. All integer data types must be converted to the float data type.
  2. All float data types must be converted to the double data type.
  3. All character data types must be converted to the integer data type.

Features of Explicit Type Casting

The various features of Explicit type casting are mentioned below:

  1. A type casting operator is necessary.
  2. The type-name should be a standard data type in the C language.
  3. The expression can be a variable, constant or an actual expression.

Converting character data type to integer data type

You can convert a character data type to an int data type via type casting. If you conduct a conversion between int and character data types, the result will be of the integer type. Between int and char data types, it is a larger data type.

Example

#include <stdio.h>

int main() {

int num = 10;

char c = ‘T’;

int sum;

sum = num + c;

printf(“Converting char to int\n”);

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

return 0;

}

Output

Converting char to int

Value of sum : 94

Explanation of Method

  1. We have created a float variable called ‘a.’
  2. Next, we have another integer data type variable, ‘b.’ Because the variables ‘a’ and ‘b’ have different data types, ‘C’ will reject such an expression and raise an error. The expression will be evaluated in some versions of ‘C,’ but the outcome will be unsatisfactory.
  3. We have typecast the variable ‘a’ of type float to avoid such scenarios. We were able to convert float to data type integer using explicit type casting methods.
  4. The value of ‘a,’ which is still a float, has been printed.
  5. The result will always be an integer ‘b’ after typecasting.

Common rules to be remember while type casting in daily practice in C

  1. Converting integers to floats is recommended.
  2. Double types should be converted from float types.
  3. Integer types should be transformed to character types.

Advantages of Type Casting in C

  1. It is done to take use of some of the characteristics of type hierarchies and type representations.
  2. It facilitates the computation of expressions comprising variables of various data kinds.
  3. It helps in converting one data type to another.
  4. It helps in making the program light-weight and easy to understand.

In-built type casting functions in C

In the C language, there are 5 different type casting functions:-

  1. atof(): a function that converts a string data type to a float data type.
  2. atoi(): a function that converts a string data type to an int data type.
  3. atbol(): a function that converts a string data type to a long data type.
  4. itoba(): a function that converts an int data type to a string data type.
  5. itoa(): a function that converts a long data type to a string data type.

Differences between type casting and type conversion

  1. Type conversion occurs when one type of data transforms into another without the need for human intervention. Type casting, on the other hand, occurs when a user converts one data type to another.
  2. You must use the operator ‘()’ for typecasting, but no operator is required with type conversion.
  3. While writing the software, type casting is done. Type conversion, on the other hand, occurs during compilation.
  4. When both data types are incompatible with each other, type casting is used. Both data types, on the other hand, must be compatible with one another.

Summing Up

  1. Type conversion is another term for typecasting.
  2. It entails the transformation of one data type into another.
  3. Type promotion is the process of converting a smaller data type into a larger one.
  4. In C, type conversion is divided into two categories: implicit and explicit type conversion.
  5. When a compatible data type is found, implicit type conversion occurs automatically.
  6. A type casting operator is required for explicit type conversion.
nv-author-image

Vaibhav Kapoor

A professional web entrepreneur, WordPress developer and digital marketing strategist with more than 4 years of experience in building a business from scratch. Knowledgeable about SEO, blogging, internet marketing, social media and website development.

Leave a Reply

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

[wpfepp_submission_form form="1"]