आज आप पढ़ने वाले हैं Type Casting in C Language in Hindi में पूरी explanation और real code examples के साथ। C programming सीखते समय एक ऐसा moment जरूर आता है जब आप दो अलग-अलग data types के बीच कोई calculation करते हैं और result गलत आ जाता है — और आप सोचते हैं कि ऐसा क्यों हुआ। इसका जवाब है Type Casting, जो C programming की एक बेहद जरूरी concept है जिसे हर beginner को ठीक से समझना चाहिए।
इस blog post में हम type casting को बिल्कुल शुरुआत से सीखेंगे। आपको कोई advanced knowledge की जरूरत नहीं है — बस basic C programming की समझ काफी है। हम सरल भाषा में, step-by-step explanation और working code programs के साथ इस topic को पूरी तरह cover करेंगे।
इस post को पढ़ने के बाद आप जानेंगे कि type casting क्या होता है, यह क्यों जरूरी है, implicit और explicit type casting में क्या फर्क है, real programs में इसे कैसे use करते हैं, और किन situations में type casting गलत result दे सकती है। तो चलिए शुरू करते हैं!
Table of Contents
Type Casting in C Language क्या होता है?
Type Casting in C Language में एक ऐसा process है जिसमें किसी variable के data type को temporarily किसी दूसरे data type में बदला जाता है। यह बदलाव सिर्फ उस expression या calculation के लिए होता है — original variable का data type वैसा ही रहता है जैसा पहले था।
आसान भाषा में कहें तो — मान लीजिए आपके पास एक int variable है जिसमें value 7 है और दूसरा int variable है जिसमें value 2 है। अगर आप इन दोनों को divide करें तो result आएगा 3 (क्योंकि integer division में decimal part cut हो जाता है)। लेकिन अगर आप चाहते हैं कि result 3.5 आए, तो आपको type casting use करनी होगी।
यही type casting की असली ताकत है — data को उसके actual type से अलग तरीके से treat करना, बिना original variable के Value को बदले।
Type Casting क्यों जरूरी है?
C एक strongly typed language है, यानी हर variable का एक fixed data type होता है और अलग-अलग types के बीच operations करते समय कई बार expected result नहीं मिलता। Type casting निम्नलिखित situations में जरूरी हो जाती है:
जब integer division में decimal result चाहिए हो — जैसे 5/2 = 2 आता है, लेकिन हमें 2.5 चाहिए।
जब function किसी specific type का argument expect करे — लेकिन हमारे पास दूसरे type का variable हो।
जब memory या performance optimize करनी हो — जैसे बड़े data type से छोटे में convert करना।
जब arithmetic operations में mixed types हों — जैसे int और float को एक साथ use करना।
Types of Type Casting in C
C language में type casting मुख्यतः दो प्रकार की होती है:
1. Implicit Type Casting (Automatic Type Conversion)
Implicit Type Casting वह होती है जो compiler खुद-ब-खुद करता है, बिना programmer के कहे। जब किसी expression में दो अलग-अलग data types होते हैं, तो compiler automatically छोटे data type को बड़े data type में convert कर देता है — ताकि data loss न हो।
इसे Type Promotion भी कहते हैं क्योंकि छोटे type को “promote” करके बड़े type में बदला जाता है।
Conversion की direction हमेशा इस order में होती है:
char → int → long → float → double
यानी char को int में, int को float में, float को double में automatically convert किया जा सकता है।
Implicit Type Casting Program:
#include <stdio.h>
int main() {
int num = 10; // Integer variable
float result; // Float variable
// Automatic conversion from int to float
result = num;
printf("Integer value: %d\n", num);
printf("Float value: %.2f\n", result);
// Integer is automatically converted to float in a mixed expression
float answer = num + 3.5;
printf("Mixed result: %.2f\n", answer);
return 0;
}
यहाँ num (int) को result (float) में assign करते समय compiler ने automatically conversion कर दी। इसी तरह num + 3.5 में num को float में promote किया गया।
ध्यान देने वाली बात: Implicit casting हमेशा safe नहीं होती। अगर आप किसी बड़े type को छोटे type में assign करें, तो data loss हो सकता है — जैसे float को int में assign करने पर decimal part खो जाता है।

2. Explicit Type Casting (Manual Type Conversion)
Explicit Type Casting वह होती है जो programmer खुद लिखता है। इसमें variable के पहले parentheses में desired data type लिखा जाता है।
Syntax:
(data_type) expression;
उदाहरण के लिए:
(float) num // num को float में convert करो
(int) 3.7 // 3.7 को int में convert करो (result: 3)
(char) 65 // 65 को char में convert करो (result: ‘A’)
Explicit Type Casting के Examples
Example 1: Integer Division में Float Result पाना
यह type casting in C language का सबसे common और important use case है।
#include <stdio.h>
int main() {
int a = 7, b = 2; // Integer variables
// Integer division
int result1 = a / b;
printf("Without type casting: %d\n", result1);
// Explicit type casting for floating-point division
float result2 = (float)a / b;
printf("With type casting: %.2f\n", result2);
return 0;
}
यहाँ (float)a ने a को temporarily float बना दिया, जिससे division का result 3.50 आया। ध्यान दें कि a की original value अभी भी int ही है — सिर्फ उस expression में float की तरह treat हुई।
Example 2: Float को Int में Convert करना
#include <stdio.h>
int main() {
float price = 99.75f; // Floating-point value
// Explicit type casting from float to int
int roundedPrice = (int)price;
printf("Original price: %.2f\n", price);
printf("After casting to int: %d\n", roundedPrice);
return 0;
}
यहाँ decimal part (.75) cut हो गया। यह truncation कहलाता है — rounding नहीं होती, बस decimal हटा दिया जाता है।
Example 3: Char को Int में Convert करना
C में हर character का एक ASCII value होती है। Type casting से char को int में convert करके उसकी ASCII value देख सकते हैं।
#include <stdio.h>
int main() {
char letter = 'A'; // Character variable
// Explicit type casting from char to int
int asciiValue = (int)letter;
printf("Character: %c\n", letter);
printf("ASCII value: %d\n", asciiValue);
// Explicit type casting from int to char
int code = 97;
char ch = (char)code;
printf("Code %d as character: %c\n", code, ch);
return 0;
}
Example 4: Percentage Calculator (Practical Use Case)
यह एक real-life example है जहाँ type casting in C language बेहद जरूरी होती है।
#include <stdio.h>
int main() {
int obtained = 456; // Marks obtained
int total = 600; // Total marks
// Integer division gives an incorrect percentage
int wrongPercent = (obtained / total) * 100;
printf("Wrong percentage: %d%%\n", wrongPercent);
// Type casting gives the correct percentage
float correctPercent = ((float)obtained / total) * 100;
printf("Correct percentage: %.2f%%\n", correctPercent);
return 0;
}
बिना type casting के 456/600 का result integer division में 0 आया क्योंकि यह 1 से कम है। Type casting ने सही result दिलाया।
Implicit vs Explicit Type Casting — Comparison
| बात | Implicit Casting | Explicit Casting |
|---|---|---|
| कौन करता है | Compiler automatically | Programmer खुद |
| Syntax | अलग से कुछ नहीं लिखना | (type) लिखना पड़ता है |
| Direction | छोटे से बड़े type में | कोई भी direction |
| Data loss | आमतौर पर नहीं | हो सकता है |
| उदाहरण | int → float automatically | (float)num, (int)3.7 |
Type Casting में होने वाली Common Mistakes
Mistake 1 — Casting सिर्फ एक operand पर:
float result = (float)(a / b); // गलत — पहले int division, फिर cast
float result = (float)a / b; // सही — पहले cast, फिर division
पहले वाले में a/b integer division हो जाती है (result: 3), फिर उसे float में cast करने से 3.00 आएगा, न कि 3.50।
Mistake 2 — Large value को छोटे type में cast करना:
int bigNum = 300;
char c = (char)bigNum; // Data loss! char की range -128 से 127 है
printf(“%d\n”, c); // Result: 44 (unexpected)
Mistake 3 — Negative float को int में cast करना:
float f = -3.9;
int i = (int)f;
printf(“%d\n”, i); // Result: -3, not -4 (truncation toward zero)
Type Casting का Complete Practical Program
#include <stdio.h>
int main() {
// Example 1: Integer to float conversion
int x = 5, y = 3;
printf("Integer division: %d\n", x / y);
printf("Float division: %.4f\n", (float)x / y);
// Example 2: Float to integer conversion
float temperature = 36.6f;
int approxTemp = (int)temperature;
printf("\nBody temperature: %.1f\n", temperature);
printf("Approximate value (int): %d\n", approxTemp);
// Example 3: Character and ASCII conversion
char grade = 'B';
printf("\nGrade: %c\n", grade);
printf("ASCII value of grade: %d\n", (int)grade);
printf("Previous character: %d = %c\n",
(int)grade - 1,
(char)((int)grade - 1));
// Example 4: Double to integer conversion
double pi = 3.14159;
int piInt = (int)pi;
printf("\nPi value: %.5f\n", pi);
printf("Pi as integer: %d\n", piInt);
return 0;
}
