Type Casting in C

Type Casting in C Language

In this article, I will discuss Type Casting in C Language with examples. Please read our previous article discussing the Data Types in C Language. As part of this article, you will learn what Type Casting in C is and why we need Typecasting in C Language with examples.

Why do we need Type Casting in C?

Type casting in C is essential for several reasons:

  • Handling Mixed Data Types: In many situations, you may need to perform operations involving variables of different data types. For instance, if you’re performing arithmetic with an integer and a float, type casting ensures the operation is handled correctly.
  • Function Argument Compatibility: Certain functions expect arguments of a specific data type. If your variable is not of that type, you must cast it to the appropriate type before passing it to the function.
  • Precision Control: Type casting can be used to control the precision of numerical calculations. For instance, converting an integer to a float can help avoid losing precision in division operations.
  • Memory Management: Sometimes, you may need to manage memory usage carefully, especially in low-resource environments like embedded systems. Type casting allows you to use only as much memory as needed by converting data to a smaller type when appropriate.
  • Interfacing with Hardware: In low-level programming, such as operating system development or embedded systems, you often need to deal with hardware that requires data in a specific format. Type casting enables you to manipulate data types to match hardware requirements.
  • Avoiding Automatic Type Promotion: In some expressions, especially complex ones, the compiler might automatically promote or change the data type of a variable (like in integer to long conversion). Type casting can prevent this automatic type promotion for more predictable behavior.
What is Type Casting in C Language?

Type casting in C is the process of converting a variable from one data type to another. This is often necessary in programming to ensure that operations are performed correctly, especially in a strongly typed language like C. Typecasting is performed by using the cast operator. If it makes sense, the compiler will automatically change one data type into another. For example, if you assign an integer value to a floating-point variable, the compiler will convert the int to float. Type conversion in c can be classified into the following two types:

Implicit Casting (Automatic Type Conversion):

The compiler automatically does this. For example, when a smaller data type (like int) is assigned to a larger data type (like float), the compiler automatically converts the int to a float. An int can be automatically converted to a float without losing information.

int i = 10;
float f = i; // Automatic conversion from int to float

Explicit Casting (Manual Type Conversion or Casting):

In this case, the programmer explicitly converts the data type, typically for operations that require specific data types. This is done by placing the target data type in parentheses before the variable.

float f = 10.5;
int i = (int)f; // Manually converting float to int
In this case, f is a float with a value of 10.5, and i is an int. By casting f to (int), the value 10.5 is truncated to 10.

Example to Understand Type Casting in C Language:
#include <stdio.h>
int main ()
  float f = 10.25;
  int i;
  i = (int) f;
  printf ("%d %f", i, f);
  return 0;
Output:TypeCasting Example in C

In the above C program, we are trying to store the floating value in an integer variable. So, we typecast it before storing it in the integer variable.

What is the Cast Operator in C?

The cast operator in C is used for explicit type casting, which allows you to convert a variable from one data type to another manually. The cast operator is represented by putting the target data type inside parentheses before the variable or value you want to convert.

Here’s the syntax for the cast operator: (target_data_type) value_to_cast;
For example, if you have a variable floatVar of type float and you want to convert it to an int, you would use the cast operator like this:
int intVar = (int) floatVar;

In this case, (int) is the cast operator. It tells the compiler to treat floatVar as an int for this operation, converting its value to an integer. This can be especially important when precision and data type compatibility are critical.

Implicit Type Conversion or Implicit Type Casting in C:

Implicit Type Casting in C, also known as automatic type conversion, occurs when the compiler automatically converts one data type into another without explicit instruction from the programmer. This usually happens in expressions involving operands of different data types. The rules governing implicit type casting are part of C’s type conversion hierarchy, which aims to prevent data loss and maintain precision as much as possible.

Here are key points about implicit type casting:

Conversion Hierarchy: In C, the data types have a hierarchy for conversion. When two different types are used in an operation, the lower-ranked type is automatically converted to the higher-ranked type. The general hierarchy (from lower to higher rank) is as follows:

  • char and short
  • int
  • unsigned int
  • long
  • unsigned long
  • float
  • double
  • long double

Operation with Mixed Types: If you perform an operation with operands of different types, the lower-ranked type is implicitly converted to the higher-ranked type. For example, the int will be automatically converted to a float in an expression combining an int and a float.

Assignment: When you assign a value of one type to a variable of another type, implicit conversion occurs if the types do not match. For example, assigning a float value to an int variable will result in the float being implicitly converted to int.

Function Calls: When passing arguments to functions, implicit conversion may occur if the argument type does not match the parameter type declared in the function.

Promotion Rules: During operations, smaller integer types (like char and short) are often promoted to int or unsigned int before the operation proceeds.

Precision and Data Loss: While implicit casting is convenient, it can lead to precision loss or unexpected results, particularly when converting from a type with higher precision (like float or double) to a type with lower precision (like int).

Avoiding Errors: Care should be taken to ensure that implicit type casting does not lead to errors or data loss, especially in critical applications.

Here’s an example demonstrating implicit Typecasting in C Language:

#include <stdio.h>

int main() {
    int i = 5;
    float f = 2.3;
    double result;

    // Implicit conversion of 'i' (int) to 'float' in the addition
    result = i + f;  
    printf("Result of i + f: %f\n", result);

    return 0;

In this example, i (an int) is implicitly converted to float when added to f, and the result is stored in result, which is of type double. The float result of i + f is then implicitly converted to double when assigned to the result.

Explicit Type Conversion or Explicit Type Casting in C:

Explicit type casting in C is a programming technique that converts a variable from one data type to another. It is done manually by the programmer using the cast operator. This casting is necessary when you need to convert a variable of one type to another type that is not automatically convertible by the compiler.

Explicit casting is used when precision needs to be controlled or changed, for example, in mathematical operations where integer division is needed instead of floating-point division. It’s also used when interfacing with functions that require parameters of a specific type. Here are some examples:

Casting an Integer to a Float
int i = 4;
float f = (float)i; // The value of f will be 4.0
Casting a Float to an Integer
float f = 5.75;
int i = (int)f; // The value of i will be 5, losing the decimal part
Casting a Char to an Integer
char c = 'A';
int i = (int)c; // The value of i will be 65 (ASCII value of 'A')
Casting an Integer to a Char
int i = 100;
char c = (char)i; // The value of c will be 'd' (ASCII character for 100)
Using Type Casting in Expressions
int a = 5, b = 2;
double result = (double)a / b; // result will be 2.5 instead of 2
Casting Pointers
int x = 10;
char *ptr = (char*)&x; // Pointer type casting

Explicit type casting is useful when you need a specific type of result from an operation, especially in mathematical expressions or when dealing with different data types like characters and integers that might represent data in various ways (e.g., ASCII values for characters). However, be cautious while casting types, as it can sometimes lead to data loss (e.g., casting a float to an int truncates the decimal part) or unexpected behavior (especially with pointer type casting).

In the next article, I will discuss Built-in Typecast Functions in C Language with Examples. In this article, I explain Typecasting in C language with examples. I hope you enjoy this article on Type Casting in C Language with Examples.  I would like to have your feedback. Please post your feedback, questions, or comments about this Type Casting in C article.

Leave a Reply

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