🟢 Beginner  ·  Lesson 10

Type Conversion & Casting

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

Type Conversion क्या है?

C में, जब एक expression में अलग-अलग data types होते हैं, compiler को एक type को दूसरे में convert करना पड़ता है। इसे type conversion या type casting कहते हैं। दो प्रकार होते हैं:

  • Implicit Conversion — compiler द्वारा automatically
  • Explicit Conversion — programmer द्वारा manually cast operator से

Implicit Conversion (Automatic)

जब आप expression में types mix करते हैं, C automatically "छोटे" type को "बड़े" type में convert करता है।

Promotion hierarchy: charintfloatdouble

C Language
int   i = 10;
float f = 3.5f;
char  c = 'A';  // ASCII 65

float r1 = i + f;    // 13.5 — int, float से float बन गया
int   r2 = c + i;    // 75 — char, int से int बन गया (65+10)
int   r3 = 3.9f;    // 3 — float, int में truncate! 4 नहीं!
int   r4 = 7 / 2;   // 3 — integer division! 3.5 नहीं!
printf("%f %d %d %d\n", r1, r2, r3, r4);
13.500000 75 3 3
⚠️ Truncation vs Rounding

Float से int में convert होने पर decimal part truncate (cut) होता है — round नहीं! 3.9 → 3 बनेगा, 4 नहीं।

Explicit Casting

Cast operator: (type)expression से manually type convert करते हैं।

C Language
int a = 7, b = 2;

// Cast के बिना: integer division
printf("%d / %d = %d\n", a, b, a / b);          // 3

// Cast के साथ: float division
printf("%d / %d = %.2f\n", a, b, (float)a / b); // 3.50

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

int ascii = 66;
printf("66 ka char = %c\n", (char)ascii); // B
7 / 2 = 3 7 / 2 = 3.50 Z ka ASCII = 90 66 ka char = B

Programs

C Language – Average (Sahi tarika)
int marks[] = {85, 90, 78, 92, 88};
int n = 5, sum = 0;
for(int i=0; i<n; i++) sum += marks[i];

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

// SAHI: pehle float mein cast karo
printf("Sahi avg: %.2f\n", (float)sum / n);
Galat avg: 86 Sahi avg: 86.60

सारांश

  • Implicit conversion — automatic, छोटा type बड़े में promote होता है
  • Explicit casting — manual, (type) operator से
  • int / intint — decimal truncate होती है, round नहीं!
  • Float division के लिए: (float)a / b
  • Float → int में convert: decimal cut होता है (truncation)
  • char और int को ASCII values के ज़रिये interchange कर सकते हैं
← Back to C Tutorial