Type casting in C: When and why to use it | Electro4u.net

03 Sep 2023 Balmiki Mandal 0 C Programming

When do we use Type Casting in C?

Type casting is a process of converting one data type into another data type. It is commonly used in the C programming language when assigning a value from one type to another. Type casting can be used to convert an integer to a float, a character to an integer, and so on. It can also be used to convert between different pointer types.

Type casting in C is done when we want to convert the data type of a variable or expression to another data type. It can be done explicitly or implicitly.

Explicit type casting is done using the cast operator, which is a unary operator that is used to explicitly convert a value from one data type to another.

Syntax for explicit typecasting in C is:

(data_type)expression

where data_type is the data type to which the expression is being converted, and expression is the value that is being converted.

For example, the following code casts the value of the variable i to an int type:

int i = 10;

float f = 1.2;

int j = (int)f; // This casts the value of f to an int type.

In this case, the value of f is a floating-point number. The cast operator converts the floating-point number to an integer, which is the truncated value of 1.2.

Implicit type casting is done automatically by the compiler when it is necessary. For example, the following code implicitly casts the value of the variable i to a float type:

C- Programming 
int i = 10;
float f = i; // This implicitly casts the value of i to a float type.

In this case, the value of i is an integer. The compiler implicitly casts the integer to a floating-point number so that the expression f = i is valid.

Type casting can be used in a variety of situations, such as:

  • To ensure that the data type of a variable is correct.
  • To optimize the performance of a program.
  • To make the code more readable and understandable.

However, typecasting should be used with caution, as it can lead to errors if it is not used correctly. For example, if a value is cast to a smaller data type, the value may be truncated.

Here are some of the rules for typecasting in C:

  • The data type of the expression must be compatible with the data type to which it is being converted.
  • If the data type of the expression is larger than the data type to which it is being converted, the value of the expression will be truncated.
  • If the data type of the expression is smaller than the data type to which it is being converted, the value of the expression will be sign-extended.

 

Program to demonstrate when we use type casting in C:

#include<stdio.h>

int main() {
  int i = 10;
  float f = 1.2;

  // Explicit typecasting
  int j = (int)f; // This casts the value of f to an int type.
  float k = (float)i; // This casts the value of i to a float type.

  // Implicit typecasting
  float l = i; // This implicitly casts the value of i to a float type.

  printf("j = %d\n", j);
  printf("k = %f\n", k);
  printf("l = %f\n", l);

  return 0;
}

This program first declares two variables, i and f, of type int and float respectively. Then, it uses the cast operator to explicitly cast the value of f to an int type and the value of i to a float type. Finally, it prints the values of j, k, and l to the console.

The output of this program is:

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.