C Programs Tutorials | IT Developer
IT Developer

C Programming - C Type Casting



Share with a Friend

C Programming - C Type Casting

Type casting in C refers to converting a variable from one data type to another explicitly. Unlike implicit type conversion (done automatically by the compiler), type casting is done manually by the programmer to achieve specific results or handle data safely.

Types of Type Casting in C

  1. Implicit Type Casting:
    • Also called type promotion.
    • Automatically done by the compiler to prevent data loss during operations involving mixed data types.
    • Example: int is automatically promoted to float in arithmetic operations.
  2. Explicit Type Casting:
    • Done manually by the programmer.
    • Involves the use of a cast operator (type).

Syntax of Explicit Type Casting

C

(type) expression;

  • type: The desired data type to cast the variable or expression.
  • expression: The variable or value to be converted.

Examples of Type Casting

  1. Casting Integers to Float

C

#include <stdio.h>

int main() {

    int a = 10, b = 3;

    float result;

    result = (float)a / b;  // Explicit cast 'a' to float

    printf("Result: %.2f\n", result);  // Output: 3.33

    return 0;

}

Here:

  • The integer a is explicitly cast to float before division to ensure accurate results.
  1. Casting Float to Integer

C

#include <stdio.h>

int main() {

    float pi = 3.14159;

    int int_pi;

    int_pi = (int)pi;  // Truncates the decimal part

    printf("Integer value: %d\n", int_pi);  // Output: 3

    return 0;

}

Here:

  • The float value pi is cast to int, resulting in truncation of the fractional part.
  1. Casting Characters to Integer

C

#include <stdio.h>

int main() {

    char ch = 'A';

    int ascii_value;

    ascii_value = (int)ch;  // Cast character to integer

    printf("ASCII Value of %c: %d\n", ch, ascii_value);  // Output: ASCII Value of A: 65

    return 0;

}

Here:

  • The char value 'A' is cast to its ASCII equivalent.
  1. Casting Integer to Unsigned

C

#include <stdio.h>

int main() {

    int a = -10;

    unsigned int b;

    b = (unsigned int)a;  // Cast signed to unsigned

    printf("Unsigned Value: %u\n", b);  // Output depends on system (e.g., 4294967286)

    return 0;

}

Here:

  • Casting a negative signed integer to unsigned results in an unexpected large value due to representation.

Use Cases of Type Casting

  1. Prevent Data Loss:
    • Ensures correct results in arithmetic operations.

C

float result = (float)5 / 2;  // Avoids integer division

  1. Accessing Data in Low-Level Programming:
    • Used to reinterpret memory or data in a different form (e.g., pointer typecasting).
  2. Compatibility Between Data Types:
    • Ensures smooth operation in functions expecting specific data types.
  3. Truncation or Rounding:
    • Allows manual control over how data is truncated or rounded.

Pitfalls of Type Casting

  1. Loss of Precision:
    • Casting from float or double to int removes the fractional part.

C

int x = (int)3.9;  // Results in 3

  1. Overflow and Underflow:
    • Casting between signed and unsigned types or to smaller types can lead to overflow.

C

char x = (char)300;  // Value wraps around in smaller range

  1. Undefined Behavior:
    • Improper casting, especially with pointers, can result in undefined behavior.

Type Casting with Pointers

In C, type casting with pointers allows interpreting data in memory differently.

Example: Casting to void* and Back

C

#include <stdio.h>

int main() {

    int num = 42;

    void *ptr = (void *)&num;  // Cast 'int*' to 'void*'

    int *int_ptr = (int *)ptr;  // Cast back to 'int*'

    printf("Value: %d\n", *int_ptr);  // Output: 42

    return 0;

}

Key Points

  • Type casting enables controlled conversion between data types.
  • Explicit casting must be used cautiously to avoid unexpected results.
  • Always ensure the target type can handle the source value without data loss or overflow.

Summary of Common Type Casting

Source Type Target Type Effect

float → int

Truncates fractional part.

 

int → float

Adds precision for operations.

 

char → int

Converts to ASCII equivalent.

 

int → char

May result in overflow for large values.

 

signed → unsigned

Converts but may wrap negative values.

 

Type casting is a powerful tool in C, enabling precision and flexibility in programming when used judiciously.