C Program to Check Alphabet Digit or Special Character – Hindi Guide If-Else Program

C Program to Check Alphabet Digit or Special Character लिखना C language सीखने वाले हर beginner के लिए एक जरूरी और बेहद practical exercise है। जब भी हम कोई form fill करते हैं या password validate करते हैं, तब यही logic काम आता है — यह जानना कि user ने जो character enter किया वह letter है, number है, या कोई special symbol। यह program C की fundamental concepts जैसे if-else, ASCII values, और scanf को एक साथ समझने का सबसे अच्छा तरीका है।

इस blog post में हम step-by-step समझेंगे कि यह program कैसे काम करता है, इसके पीछे की logic क्या है, और code की हर line का क्या मतलब है। चाहे आप पहली बार C सीख रहे हों या exam की तैयारी कर रहे हों, यह guide आपके लिए पूरी तरह उपयोगी है।

आगे आप जानेंगे — program का complete code, उसकी line-by-line explanation, output examples, ASCII values की भूमिका, और आम गलतियाँ जो beginners करते हैं। यह post आपको न सिर्फ code copy करने देगी, बल्कि पूरी logic को समझाएगी ताकि आप खुद से ऐसे programs बना सकें।

C Program to Check Alphabet Digit or Special Character क्या होता है?

C language में character checking एक ऐसी technique है जिसमें हम user से एक single character input लेते हैं और यह पता लगाते हैं कि वह character किस category में आता है। तीन मुख्य categories होती हैं:

  • Alphabet — A से Z (uppercase) या a से z (lowercase)
  • Digit0 से 9
  • Special Symbol — बाकी सभी characters जैसे @, #, $, %, !, &, * आदि

यह logic real-world applications में बहुत काम आती है, जैसे username validation, password strength checking, और input sanitization।

Program Code

#include <stdio.h>


int main() {
    char ch;   // Variable to store a single character


    printf("Enter a character: ");
    scanf(" %c", &ch);


    // Check whether the character is an alphabet
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
        printf("The entered character is an Alphabet.\n");
    }
    // Check whether the character is a digit
    else if (ch >= '0' && ch <= '9') {
        printf("The entered character is a Digit.\n");
    }
    // Otherwise, it is a special symbol
    else {
        printf("The entered character is a Special Symbol.\n");
    }


    return 0;
}

Output (Example 1):

▶️ Output Result
Enter a character: A The entered character is an Alphabet.

Output (Example 2):

▶️ Output Result
Enter a character: 7 The entered character is a Digit.

Output (Example 3):

▶️ Output Result
Enter a character: @ The entered character is a Special Symbol.

यह code simple, clean, और beginner-friendly है। आइए अब इसे line-by-line समझते हैं।

Program की Line-by-Line Explanation

1. Header File — #include <stdio.h>

यह program की सबसे पहली line है। stdio.h एक standard input-output header file है जो C compiler को यह बताती है कि हम printf() और scanf() जैसे built-in functions use करने वाले हैं। बिना इस line के program compile नहीं होगा।

2. Variable Declaration — char ch;

यहाँ char data type का एक variable ch declare किया गया है। char data type exactly 1 byte memory लेता है और एक single character store करने के लिए use होता है। चूँकि हमें user से सिर्फ एक character लेना है, इसलिए char सबसे उपयुक्त choice है।

3. Input लेना — scanf(” %c”, &ch);

printf(“Enter character: “);

scanf(” %c”, &ch);

  • %c एक format specifier है जो character input या output के लिए use होता है।
  • &ch का मतलब है variable ch का memory address — यहाँ input store होगा।
  • Space ” %c”%c से पहले एक space लिखा गया है। यह बहुत important है क्योंकि यह 

buffer में पहले से मौजूद किसी भी whitespace या newline character (\n) को ignore कर देता है। बिना इस space के program incorrect behavior दिखा सकता है।

4. Alphabet Check करना

if ((ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’))

यह condition दो भागों में है:

  • ch >= ‘a’ && ch <= ‘z’ — छोटे अक्षरों (lowercase) की जाँच करता है
  • ch >= ‘A’ && ch <= ‘Z’ — बड़े अक्षरों (uppercase) की जाँच करता है
  • दोनों को || (OR operator) से जोड़ा गया है, यानी अगर इनमें से कोई भी एक condition सच हो, तो character alphabet माना जाएगा।

C में characters internally ASCII values के रूप में store होते हैं। जैसे ‘A’ की ASCII value 65, ‘Z’ की 90, ‘a’ की 97, और ‘z’ की 122 है। इसलिए यह range comparison बिल्कुल सही काम करती है।

5. Digit Check करना

else if (ch >= ‘0’ && ch <= ‘9’)

अगर पहली condition false हो, तो यह check होता है। ‘0’ की ASCII value 48 और ‘9’ की ASCII value 57 है। अगर entered character इन दोनों के बीच है, तो वह digit है।

यह ध्यान रखें कि यहाँ ‘0’ (character) और 0 (integer) में फर्क है। हम character compare कर रहे हैं, इसलिए quotes जरूरी हैं।

6. Special Symbol — else block

else
{
	printf("Entered character is a Special Symbol");
}

अगर character न alphabet है और न digit, तो यह automatically special symbol होगा। इसमें आते हैं — @, #, $, %, &, *, !, ^, (, ), space आदि।

InputOutput
AEntered character is an Alphabet
zEntered character is an Alphabet
5Entered character is a Digit
@Entered character is a Special Symbol
#Entered character is a Special Symbol
0Entered character is a Digit

ASCII Values की भूमिका

C Program to Check Alphabet Digit or Special Character को समझने के लिए ASCII (American Standard Code for Information Interchange) जानना जरूरी है। C में हर character एक number के रूप में store होता है।

Character RangeASCII Range
A – Z65 – 90
a – z97 – 122
0 – 948 – 57
Special symbolsबाकी सभी values

इसीलिए जब हम ch >= ‘a’ लिखते हैं, तो C compiler actually ch >= 97 check करता है।

isalpha(), isdigit() Functions से Alternative तरीका

C language में ctype.h header file का उपयोग करके यह काम और भी आसान तरीके से किया जा सकता है:

#include <stdio.h>
#include <ctype.h>


int main() {
    char ch;   // Variable to store a single character


    printf("Enter a character: ");
    scanf(" %c", &ch);


    // Check whether the character is an alphabet
    if (isalpha(ch)) {
        printf("Alphabet\n");
    }
    // Check whether the character is a digit
    else if (isdigit(ch)) {
        printf("Digit\n");
    }
    // Otherwise, it is a special symbol
    else {
        printf("Special Symbol\n");
    }


    return 0;
}

Output (Example 1):

▶️ Output Result
Enter a character: A Alphabet

Output (Example 2):

▶️ Output Result
Enter a character: 9 Digit

Output (Example 3):

▶️ Output Result
Enter a character: @ Special Symbol
  • isalpha(ch) — alphabet check करता है
  • isdigit(ch) — digit check करता है

हालांकि यह तरीका छोटा है, लेकिन beginners को पहले manual if-else method से ही शुरुआत करनी चाहिए ताकि logic पूरी तरह समझ में आए।

Common Mistakes जो Beginners करते हैं

C program को लिखते समय नए students अक्सर कुछ गलतियाँ करते हैं जिन्हें जानना जरूरी है:

  • scanf में space न देनाscanf(“%c”, &ch) की जगह scanf(” %c”, &ch) लिखना जरूरी है, वरना newline character buffer में रह जाता है।
  • Single quotes भूल जानाch >= a गलत है, सही है ch >= ‘a’
  • || की जगह && use करना — Alphabet check में uppercase और lowercase दोनों को || से जोड़ना होता है, && से नहीं।
  • else if की जगह सिर्फ if लिखना — इससे logic गलत हो जाती है क्योंकि हर condition independently check होगी।

Conclusion (निष्कर्ष )

C Program to Check Alphabet Digit or Special Character एक ऐसा foundational program है जो C language की कई जरूरी concepts को एक साथ practice करवाता है — जैसे char data type, if-else conditions, ASCII values, और formatted input। इस program को अच्छी तरह समझ लेने के बाद आप character-based problems को confidence के साथ solve कर पाएंगे। यह logic न सिर्फ exam में काम आती है, बल्कि real software development में भी इसका directly उपयोग होता है।

अब आपकी बारी है — इस program को अपने compiler में type करके run करें, अलग-अलग characters input करके output देखें, और ctype.h वाले alternative method को भी try करें। अगर आपको यह post helpful लगी हो, तो इसे अपने classmates और fellow programmers के साथ share करें। C language के ऐसे ही और programs और explanations के लिए हमारे blog को follow करते रहें — आपकी programming journey यहाँ और भी आसान बनेगी।

Frequently Asked Questions

Q 1: यह program किस concept पर based है?
Ans: यह C Program to Check Alphabet, Digit or Special Character मुख्यतः if-else if-else conditional statements और ASCII character comparison पर based है। इसमें char data type और scanf का सही उपयोग सबसे महत्वपूर्ण है।
Q 2: scanf में space क्यों लगाते हैं?
Ans: scanf(” %c”, &ch) में space इसलिए लगाया जाता है ताकि input buffer में पहले से मौजूद whitespace या newline character अनदेखा हो जाए। इससे program सही character read कर पाता है, खासकर जब पहले कोई और input लिया गया हो।
Q 3: क्या ctype.h functions use करना बेहतर है?
Ans: दोनों तरीके सही हैं। ctype.h के functions जैसे isalpha() और isdigit() code को छोटा बनाते हैं, लेकिन beginners के लिए manual if-else logic पहले सीखना अधिक फायदेमंद है क्योंकि इससे underlying logic की समझ बनती है।
Q 4: Space character किस category में आएगा?
Ans: Space (‘ ‘) न alphabet है, न digit — इसलिए यह Special Symbol category में count होगा। इसकी ASCII value 32 होती है।
Q 5: क्या यही logic password validation में use होती है?
Ans: हाँ, यही basic logic password strength checker का आधार है। अगर password में alphabets, digits, और special symbols तीनों हों, तो उसे strong माना जाता है। इस program की condition को थोड़ा modify करके आप एक simple password validator भी बना सकते हैं।

Leave a Comment

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

Scroll to Top