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): char → int → unsigned int → long → float → double
#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; }
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
(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'
#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; }
Conversion Rules Summary
| Expression | Result Type | Example | Output |
|---|---|---|---|
| int + int | int | 7 / 2 | 3 (truncated) |
| int + float | float | 7 + 2.5f | 9.5 |
| float + double | double | 2.5f + 1.0 | 3.5 (double) |
| char op int | int | 'A' + 1 | 66 |
| (float)int / int | float | (float)7/2 | 3.500000 |
| (int)float | int | (int)3.9 | 3 (truncated) |
Complete Programs
Average with Correct Float Division
#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; }
ASCII Value Converter
#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; }
Summary
- Implicit conversion — automatic, smaller type promoted to larger (widening)
- Explicit casting — manual using
(type)operator int / int→int(decimal part truncated — not rounded!)- Use
(float)a / bto get float division result - Narrowing (float → int) truncates decimal part
charandintcan be interchanged using ASCII values
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: char → int → float → double
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);
Float से int में convert होने पर decimal part truncate (cut) होता है — round नहीं! 3.9 → 3 बनेगा, 4 नहीं।
Explicit Casting
Cast operator: (type)expression से manually type convert करते हैं।
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
Programs
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);
सारांश
- Implicit conversion — automatic, छोटा type बड़े में promote होता है
- Explicit casting — manual,
(type)operator से int / int→int— decimal truncate होती है, round नहीं!- Float division के लिए:
(float)a / b - Float → int में convert: decimal cut होता है (truncation)
charऔरintको ASCII values के ज़रिये interchange कर सकते हैं