Tutorials Logic, IN info@tutorialslogic.com

C Type Casting Implicit Explicit Conversion

Conversion Rules

C performs implicit arithmetic conversions and permits explicit casts, but a cast does not make an out-of-range value, invalid pointer, or discarded qualifier safe. Convert only after establishing the source range and the destination’s representation requirements.

What is Type Casting?

Type casting is the process of converting a value from one data type to another. In C, this happens in two ways:

Understanding type casting is critical to avoid data loss, precision errors, and unexpected behaviour in arithmetic operations.

  • Implicit Casting (Type Promotion) - done automatically by the compiler when mixing types in an expression.
  • Explicit Casting - done manually by the programmer using the cast operator (type).

Implicit Type Conversion (Promotion)

When operands of different types appear in an expression, C automatically promotes the smaller type to the larger type. The promotion hierarchy is:

Rank (lowest -> highest) Type
1 char, short
2 int
3 unsigned int
4 long
5 unsigned long
6 float
7 double
8 long double

Implicit Type Promotion

Implicit Type Promotion
#include <stdio.h>

int main() {
    int   a = 5;
    float b = 2.5;

    // int + float -> float (a is promoted to float automatically)
    float result = a + b;
    printf("5 + 2.5 = %.1f\n", result);  // 7.5

    // Integer division - both operands are int, result is int
    int x = 7, y = 2;
    printf("7 / 2 = %d\n", x / y);       // 3 (truncated, NOT 3.5)

    // char is promoted to int in arithmetic
    char c = 'A';  // ASCII 65
    printf("'A' + 1 = %d\n", c + 1);     // 66

    // Mixed: int * double -> double
    int   i = 3;
    double d = 1.5;
    printf("3 * 1.5 = %f\n", i * d);     // 4.500000

    return 0;
}

/*
Output:
5 + 2.5 = 7.5
7 / 2 = 3
'A' + 1 = 66
3 * 1.5 = 4.500000
*/

Explicit Type Casting

Use the cast operator (type)expression to manually convert a value. This is necessary when you want to control the conversion - for example, getting a floating-point result from integer division.

Explicit Casting Examples

Explicit Casting Examples
#include <stdio.h>

int main() {
    // Fix integer division with explicit cast
    int a = 7, b = 2;
    float result = (float)a / b;   // cast a to float BEFORE division
    printf("(float)7 / 2 = %.2f\n", result);  // 3.50

    // double to int - truncates decimal part (no rounding)
    double pi = 3.14159;
    int truncated = (int)pi;
    printf("(int)3.14159 = %d\n", truncated);  // 3

    // int to char - takes the ASCII value
    int code = 72;
    char letter = (char)code;
    printf("(char)72 = %c\n", letter);  // H

    // Pointer casting - void* to int*
    void *vptr;
    int  num = 100;
    vptr = #
    int *iptr = (int*)vptr;
    printf("Via void*: %d\n", *iptr);  // 100

    // Overflow example - be careful!
    int big = 300;
    char small = (char)big;  // 300 % 256 = 44 -> ','
    printf("(char)300 = %d ('%c')\n", small, small);  // 44 ','

    return 0;
}

/*
Output:
(float)7 / 2 = 3.50
(int)3.14159 = 3
(char)72 = H
Via void*: 100
(char)300 = 44 (',')
*/

Common Pitfalls

Pitfall Wrong Correct
Integer division losing decimal float r = 7/2; -> 3.0 float r = (float)7/2; -> 3.5
Casting after division (too late) (float)(7/2) -> 3.0 (float)7/2 -> 3.5
Overflow on narrowing cast (char)300 -> 44 Check range before casting
Signed/unsigned mismatch (unsigned int)-1 -> 4294967295 Avoid mixing signed/unsigned

Practical: Average Calculator with Correct Casting

Practical: Average Calculator with Correct Casting
#include <stdio.h>

int main() {
    int scores[] = {85, 92, 78, 95, 88};
    int n = 5;
    int sum = 0;

    for (int i = 0; i < n; i++) {
        sum += scores[i];
    }

    // Without cast: integer division -> wrong average
    printf("Wrong average:   %d\n", sum / n);          // 87

    // With cast: float division -> correct average
    printf("Correct average: %.2f\n", (float)sum / n); // 87.60

    return 0;
}

/*
Output:
Wrong average:   87
Correct average: 87.60
*/

Narrowing and Pointers

Before narrowing an integer, compare against the destination limits from <limits.h> or <stdint.h>. Floating-to-integer conversion requires a finite value within range; otherwise behavior is undefined. Keep signed and unsigned comparisons deliberate because a negative signed value may convert to a large unsigned value.

Do not cast away const and then modify an object originally defined as const. Converting between object-pointer types can create alignment and aliasing problems; use the declared type, character types for byte inspection, or memcpy when moving representation safely.

Before you move on

C Type Casting Implicit Explicit Conversion Mastery Check

4 checks
  • Type casting is the process of converting a value from one data type to another.
  • In C, this happens in two ways.
  • When operands of different types appear in an expression, C automatically promotes the smaller type to the larger type.
  • Use the cast operator (type)expression to manually convert a value.
Next Step
Next Practice

Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.