C Language Login Program एक ऐसा beginner-friendly project है जो आपको user authentication की basic concept को practically समझने में मदद करता है। अगर आप C programming सीख रहे हैं और real-world applications बनाने की शुरुआत करना चाहते हैं, तो यह program आपके लिए एक बेहतरीन starting point है। इसमें आप सीखेंगे कि कैसे username और password को store करके user input से compare किया जाता है।
इस blog post में हम step by step पूरे program को समझेंगे — header files से लेकर output तक। आप जानेंगे कि strcmp() function कैसे काम करता है, if-else conditions कैसे apply होती हैं, और logical operator && का use क्यों जरूरी है। यह सभी concepts C programming के foundation हैं जो आगे चलकर बड़े projects में काम आते हैं।
चाहे आप किसी exam के लिए prepare कर रहे हों, college assignment complete करनी हो, या simply programming सीखनी हो — इस post को पढ़ने के बाद आप खुद एक working login program लिख पाएंगे। तो चलिए शुरू करते हैं।
Table of Contents
C Language Login Program क्या होता है?
C Language Login Program एक ऐसा program है जिसमें एक predefined username और password होता है। जब user अपना username और password enter करता है, तो program उसे stored values से compare करता है। अगर दोनों match करें तो “Login Successfully” दिखता है, वरना “Invalid Credential!” का message आता है।
यह concept real-world banking apps, website login forms, और software authentication systems में use होता है। C में इसे सीखना आपकी programming thinking को मजबूत बनाता है।
Program Statement
एक ऐसा C Language Login Program बनाइये जिसमें:
- पहले से एक username “admin” और password “123456” defined हो।
- User से username और password input लिया जाए।
- अगर दोनों सही हों तो “Login Successfully” दिखे।
- अगर कोई भी गलत हो तो “Invalid Credential!” दिखाया जाए।
Program Code
#include<stdio.h>
#include<string.h>
int main()
{
char user[20] = "admin";
char password[20] = "123456";
char user1[20];
char password1[20];
printf("\nEnter User name : ");
scanf("%s", user1);
printf("\nEnter Password : ");
scanf("%s", password1);
if(strcmp(user, user1) == 0 && strcmp(password, password1) == 0)
printf("\nLogin Successfully");
else
printf("Invalid Credential!");
return 0;
}
Program Output
Case 1: सही Input देने पर
Case 2: गलत Input देने पर
Step by Step Code Explanation
अब हम इस C Language Login Program को हर step में detail से समझते हैं ताकि आपको कोई confusion न रहे।
Step 1 – Header Files Include करना
#include<stdio.h>
#include<string.h>
किसी भी C program की शुरुआत header files से होती है।
- stdio.h — यह Standard Input Output library है। इसमें printf() और scanf() जैसे functions होते हैं जिनसे हम screen पर कुछ print करते हैं और user से input लेते हैं।
- string.h — यह String library है। इसमें strcmp() function होता है जो दो strings को आपस में compare करता है। बिना इसे include किये strcmp() काम नहीं करेगा।
Step 2 – Variables Declare करना
char user[20] = "admin";
char password[20] = "123456";
char user1[20];
char password1[20];यहाँ चार character arrays (strings) declare किये गए हैं:
- user[20] — इसमें predefined username “admin” store किया गया है। Size 20 इसलिए दी गई है ताकि 20 characters तक का username store हो सके।
- password[20] — इसमें predefined password “123456” store है।
- user1[20] — यह empty variable है जिसमें user द्वारा enter किया गया username store होगा।
- password1[20] — इसमें user का enter किया गया password store होगा।
Key Point: C language में strings को char array की form में store किया जाता है क्योंकि C में built-in string data type नहीं होती।
Step 3 – User से Input लेना
printf("\nEnter User name : ");
scanf("%s", user1);
printf("\nEnter Password : ");
scanf("%s", password1);- printf() screen पर message print करता है।
- scanf(“%s”, user1) user द्वारा type किया गया username user1 variable में store कर लेता है।
- %s format specifier string input के लिए use होता है।
- यही process password के लिए भी repeat होती है।
ध्यान दें: scanf() में string के लिए & operator use नहीं होता क्योंकि array का नाम खुद ही उसका address होता है।
Step 4 – Condition Check करना
if (strcmp(user, user1) == 0 && strcmp(password, password1) == 0)
printf("\nLogin Successfully");
else
printf("Invalid Credential!");यह इस पूरे C Language Login Program का सबसे important हिस्सा है। इसे ध्यान से समझें:
strcmp() function कैसे काम करता है?
- strcmp() दो strings को character by character compare करता है।
- अगर दोनों strings बिल्कुल same हों → 0 return करता है।
- अगर पहली string छोटी हो → negative value return करता है।
- अगर पहली string बड़ी हो → positive value return करता है।
- इसलिए हम check करते हैं कि strcmp() का result == 0 है या नहीं।
&& (Logical AND) Operator क्यों use किया?
Login successful तभी होना चाहिए जब username और password दोनों सही हों। इसलिए && operator use किया गया। अगर कोई भी एक गलत हो, तो पूरी condition false हो जाती है और else block execute होता है।
Step 5 – Output Print होना
- Condition true होने पर → Login Successfully print होता है।
- Condition false होने पर → Invalid Credential! print होता है।
यह simple लेकिन बहुत effective logic है जो real authentication systems की नींव है।
इस Program से क्या सीखते हैं?
इस C Language Login Program को लिखने के बाद आपको इन concepts की practical knowledge मिल जाती है:
- String Declaration और Initialization — char arrays को कैसे declare और initialize करते हैं।
- String Input — scanf() से string कैसे लेते हैं।
- strcmp() function — दो strings को compare करने का तरीका।
- if-else Conditional Statement — condition के basis पर अलग-अलग output देना।
- Logical AND (&&) Operator — दो conditions को एक साथ check करना।
Program को और बेहतर कैसे बनाएं?
अगर आप इस program को थोड़ा और advanced बनाना चाहते हैं, तो ये ideas try करें:
- Multiple Login Attempts — for loop का use करके user को 3 बार try करने का मौका दें।
- Password Masking — password type करते समय characters की जगह * दिखाएं (getch() function से)।
- Multiple Users — arrays of strings use करके multiple users का data store करें।
- File Handling — username और password को एक file में store करें और वहाँ से read करें।
ये improvements करने से आप C programming में और अधिक confident बनेंगे।
Important Points याद रखें
- C language में strings char arrays होती हैं, इसलिए == operator से directly compare नहीं कर सकते।
- strcmp() का result 0 होने का मतलब है strings equal हैं।
- scanf(“%s”) में array का नाम directly देते हैं, & नहीं।
- string.h include करना अनिवार्य है।
- Login logic में && operator का सही use बहुत जरूरी है।
Conclusion ( निष्कर्ष )
C Language Login Program एक ऐसा project है जो दिखने में simple लगता है लेकिन इसमें C programming के कई important concepts एक साथ use होते हैं — string handling, strcmp(), if-else, और logical operators। इस program को खुद type करके compile और run करें, और फिर उसमें modifications करें जैसे multiple attempts या multiple users add करना। Practice करने से ही programming skills मजबूत होती हैं।
अगर यह post आपको helpful लगी हो तो इसे अपने classmates और दोस्तों के साथ share करें जो C programming सीख रहे हैं। नीचे comment करके बताएं कि आपको कौन सा step सबसे useful लगा या अगर कोई doubt है तो पूछें। हम हर comment का जवाब देते हैं। C programming के और ऐसे practical programs के लिए हमारी website को bookmark करें और नई posts miss मत करें।
