Type Casting in C Language in Hindi – Complete Guide with Examples (हिंदी)

आज आप पढ़ने वाले हैं 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 दे सकती है। तो चलिए शुरू करते हैं!

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;
}
▶️ Output Result
Integer value: 10 Float value: 10.00 Mixed result: 13.50

यहाँ 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 खो जाता है।

Type Casting in C Language in Hindi

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;
}
▶️ Output Result
Without type casting: 3 With type casting: 3.50

यहाँ (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;
}
▶️ Output Result
Original price: 99.75 After casting to int: 99

यहाँ 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;
}
▶️ Output Result
Character: A ASCII value: 65 Code 97 as character: a

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;
}
▶️ Output Result
Wrong percentage: 0% Correct percentage: 76.00%

बिना type casting के 456/600 का result integer division में 0 आया क्योंकि यह 1 से कम है। Type casting ने सही result दिलाया।

Implicit vs Explicit Type Casting — Comparison

बातImplicit CastingExplicit Casting
कौन करता हैCompiler automaticallyProgrammer खुद
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;
}
▶️ Output Result
Integer division: 1 Float division: 1.6667Body temperature: 36.6 Approximate value (int): 36Grade: B ASCII value of grade: 66 Previous character: 65 = APi value: 3.14159 Pi as integer: 3Body temperature: 36.6 Approximate (int): 36 Grade: B ASCII of grade: 66 Next grade ASCII: 65 = A Pi value: 3.14159 Pi as int: 3

Conclusion ( निष्कर्ष )

तो दोस्तों, इस पूरी post में हमने Type Casting in C Language in Hindi में आसान Explanation के साथ बिल्कुल शुरुआत से समझा। हमने देखा कि type casting क्या होती है, compiler खुद कब conversion करता है (Implicit), और programmer को कब manually cast करनी पड़ती है (Explicit)। साथ ही real code examples से यह भी समझा कि percentage calculator जैसी simple चीज़ में भी बिना type casting के result कितना गलत आ सकता है।

Type casting C programming की एक ऐसी concept है जो देखने में छोटी लगती है, लेकिन real programs में बहुत बड़ा फर्क करती है। अगर आप integer division में गलत result से परेशान हैं, या float और int के बीच calculations में confusion हो रही है — तो अब आप जानते हैं कि solution क्या है।

याद रखें — हमेशा सही operand पर cast करें, data loss के बारे में पहले से सोचें, और unnecessary casting से बचें। इन simple बातों को ध्यान में रखकर आप type casting को confidently use कर पाएंगे।

अगर यह post आपके लिए helpful रही हो तो इसे अपने दोस्तों के साथ share करें जो C programming सीख रहे हैं। और अगर आपका कोई सवाल हो तो नीचे comment में जरूर पूछें — हम हर सवाल का जवाब देते हैं!

Frequently Asked Questions

Q 1: Type Casting in C Language क्या होता है?
Ans: Type Casting एक ऐसा process है जिसमें किसी variable के data type को temporarily किसी दूसरे data type में convert किया जाता है। यह conversion सिर्फ उस expression के लिए होती है — original variable का data type नहीं बदलता। यह दो प्रकार की होती है: Implicit (compiler द्वारा) और Explicit (programmer द्वारा)।
Q 2: Implicit और Explicit Type Casting में क्या फर्क है?
Ans: Implicit Type Casting compiler खुद करता है जब छोटे data type को बड़े type में convert करना हो — जैसे int को float में। इसमें data loss नहीं होता। Explicit Type Casting programmer खुद `(type)` syntax लिखकर करता है, और इसमें किसी भी direction में conversion हो सकती है — जैसे `(int)3.7` या `(float)num`। Explicit casting में data loss संभव है।
Q 3: Integer Division में float result कैसे पाएं?
Ans: Integer division में float result पाने के लिए numerator या denominator को explicitly float में cast करना होगा। जैसे `(float)a / b` लिखने से पहले `a` float बन जाता है और division का result float में आता है। ध्यान रहे कि `(float)(a/b)` लिखने से integer division पहले होगी और फिर cast होगी — यह गलत होगा।
Q 4: Type Casting में Data Loss कब होता है?
Ans: Data loss तब होता है जब बड़े या more precise data type को छोटे type में cast किया जाए। जैसे float को int में cast करने पर decimal part cut हो जाता है (99.75 → 99)। Double को float में cast करने पर precision कम हो जाती है। long को char में cast करने पर range exceed होने पर unexpected values आ सकती हैं। इसलिए ऐसी casting सोच-समझकर करें।
Q 5: Char को Int में cast क्यों करते हैं?
Ans: C language में हर character को ASCII table के according एक integer value दी गई है। जैसे ‘A’ की ASCII value 65 है और ‘a’ की 97 है। `(int)` cast लगाकर किसी character की ASCII value निकाली जा सकती है। यह character comparison, encryption, और string manipulation जैसे tasks में बहुत useful होता है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top