🟢 Beginner  ·  Lesson 10

Type Conversion & Casting

What is Type Conversion?

In C, when an expression involves different data types, the compiler needs to convert one type to another. This is called type conversion (also called type casting). There are two kinds:

  • Implicit Conversion — done automatically by the compiler
  • Explicit Conversion — done manually by the programmer using cast operator

Implicit Conversion (Automatic / Widening)

When you mix types in an expression, C automatically converts the "smaller" type to the "larger" type to avoid data loss. This happens automatically — no special syntax needed.

Promotion hierarchy (lower → higher): charintunsigned intlongfloatdouble

C Language – Implicit Conversion
#include <stdio.h>
int main() {
    int   i = 10;
    float f = 3.5f;
    char  c = 'A';  // ASCII value 65

    // int + float → float (int promoted to float)
    float result1 = i + f;
    printf("10 + 3.5 = %.1f\n", result1);  // 13.5

    // char + int → int (char promoted to int)
    int result2 = c + i;
    printf("'A' + 10 = %d\n", result2);    // 75 (65+10)

    // Assigning float to int → TRUNCATION (not rounding!)
    int truncated = 3.9f;
    printf("(int)3.9 = %d\n", truncated);   // 3 (not 4!)

    // int / int → int (no decimal!)
    int div = 7 / 2;
    printf("7 / 2 = %d\n", div);             // 3 (not 3.5!)

    return 0;
}
10 + 3.5 = 13.5 'A' + 10 = 75 (int)3.9 = 3 7 / 2 = 3
⚠️ Narrowing Conversion Warning

Assigning a larger type to a smaller type (float → int) is narrowing conversion. The decimal part is truncated (cut off, NOT rounded). 3.9 becomes 3, not 4! The compiler may warn but will allow it.

Explicit Conversion (Type Casting)

You force a type conversion using the cast operator: (type)expression

Syntax
(target_type) expression
// Examples:
(float) 7      // converts 7 to 7.0
(int) 3.9     // converts 3.9 to 3
(char) 65     // converts 65 to 'A'
C Language – Explicit Casting
#include <stdio.h>
int main() {
    int a = 7, b = 2;

    // Without cast: integer division
    printf("Without cast: %d / %d = %d\n", a, b, a / b);   // 3

    // With cast: float division
    printf("With cast:    %d / %d = %.2f\n", a, b, (float)a / b); // 3.50

    // char to int
    char ch = 'Z';
    printf("ASCII of Z = %d\n", (int)ch);   // 90

    // int to char
    int ascii = 66;
    printf("Char 66 = %c\n", (char)ascii);  // B

    // double to int (truncation)
    double d = 9.99;
    printf("(int)9.99 = %d\n", (int)d);     // 9

    return 0;
}
Without cast: 7 / 2 = 3 With cast: 7 / 2 = 3.50 ASCII of Z = 90 Char 66 = B (int)9.99 = 9

Conversion Rules Summary

ExpressionResult TypeExampleOutput
int + intint7 / 23 (truncated)
int + floatfloat7 + 2.5f9.5
float + doubledouble2.5f + 1.03.5 (double)
char op intint'A' + 166
(float)int / intfloat(float)7/23.500000
(int)floatint(int)3.93 (truncated)

Complete Programs

Average with Correct Float Division

C Language
#include <stdio.h>
int main() {
    int marks[] = {85, 90, 78, 92, 88};
    int n = 5, sum = 0;

    for (int i = 0; i < n; i++) sum += marks[i];

    // WRONG: int / int = int
    printf("Wrong avg: %d\n", sum / n);

    // CORRECT: cast to float first
    printf("Correct avg: %.2f\n", (float)sum / n);

    return 0;
}
Wrong avg: 86 Correct avg: 86.60

ASCII Value Converter

C Language
#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);
    printf("'%c' ASCII = %d\n", ch, (int)ch);

    // Uppercase from lowercase
    if (ch >= 'a' && ch <= 'z') {
        char upper = (char)(ch - 32);
        printf("Uppercase: %c\n", upper);
    }
    return 0;
}
Enter a character: g 'g' ASCII = 103 Uppercase: G

Summary

  • Implicit conversion — automatic, smaller type promoted to larger (widening)
  • Explicit casting — manual using (type) operator
  • int / intint (decimal part truncated — not rounded!)
  • Use (float)a / b to get float division result
  • Narrowing (float → int) truncates decimal part
  • char and int can be interchanged using ASCII values
🏋️ Practice

Write programs: (1) Correct average of 5 integers (2) Find percentage: (marks/total)*100 — avoid integer division (3) Temperature conversion: Celsius to Fahrenheit with float precision (4) Convert lowercase to uppercase using casting

Frequently Asked Questions

What is type conversion in C?
Type conversion means changing a value from one data type to another, such as turning an int into a float. It happens either automatically (implicit) or when you request it (explicit), and it lets values of different types work together in an expression.
What is the difference between implicit and explicit type conversion?
Implicit conversion is done automatically by the compiler when types are mixed, such as promoting an int to a float in an expression. Explicit conversion, or casting, is when you deliberately convert a value using a cast like (int)x.
What is type casting in C?
Type casting is explicit type conversion, where you write the target type in parentheses before a value, such as (float)5 to treat the integer 5 as a float. It gives you direct control over how a value is interpreted.
What is type promotion in C?
Type promotion is an automatic conversion of a smaller type to a larger one during an expression, for example a char or short being promoted to int. It ensures calculations happen in a consistent, wide-enough type.
Why does integer division lose the decimal part in C?
When both operands are integers, C performs integer division and discards the fractional part, so 5 / 2 gives 2, not 2.5. To keep the decimals you convert at least one operand to a floating type, for example (float)5 / 2.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.