🟢 Beginner · Lesson 10
Type Conversion और 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
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 / int→int— decimal truncate होती है, round नहीं!- Float division के लिए:
(float)a / b - Float → int में convert: decimal cut होता है (truncation)
charऔरintको ASCII values के ज़रिये interchange कर सकते हैं
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में type conversion क्या है?
Type conversion का मतलब किसी value को एक data type से दूसरे में बदलना है, जैसे
int को float बनाना। यह या तो अपने आप (implicit) होता है या जब आप माँगें (explicit), और यह अलग types की values को एक expression में साथ काम करने देता है।implicit और explicit type conversion में क्या अंतर है?
Implicit conversion compiler द्वारा अपने आप होता है जब types मिलते हैं, जैसे expression में
int को float में बढ़ाना। Explicit conversion, या casting, तब है जब आप जानबूझकर (int)x जैसे cast से value बदलते हैं।C में type casting क्या है?
Type casting explicit type conversion है, जहाँ आप value से पहले कोष्ठकों में target type लिखते हैं, जैसे integer 5 को float मानने को
(float)5। यह आपको सीधा नियंत्रण देता है कि value को कैसे समझा जाए।C में type promotion क्या है?
Type promotion expression के दौरान छोटे type का बड़े में अपने आप बदलना है, जैसे
char या short का int में बढ़ना। यह सुनिश्चित करता है कि गणनाएँ एक सुसंगत, पर्याप्त-चौड़े type में हों।C में integer division दशमलव हिस्सा क्यों खो देता है?
जब दोनों operands integers हों, C integer division करता है और भिन्नात्मक हिस्सा हटा देता है, तो
5 / 2 2 देता है, 2.5 नहीं। दशमलव रखने को आप कम-से-कम एक operand को floating type में बदलते हैं, जैसे (float)5 / 2।