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 बना सकें।
Table of Contents
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)
- Digit — 0 से 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 (Example 2):
Output (Example 3):
यह 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 आदि।
| Input | Output |
|---|---|
| A | Entered character is an Alphabet |
| z | Entered character is an Alphabet |
| 5 | Entered character is a Digit |
| @ | Entered character is a Special Symbol |
| # | Entered character is a Special Symbol |
| 0 | Entered 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 Range | ASCII Range |
|---|---|
| A – Z | 65 – 90 |
| a – z | 97 – 122 |
| 0 – 9 | 48 – 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 (Example 2):
Output (Example 3):
- 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 यहाँ और भी आसान बनेगी।
