C Language Login Program एक simple लेकिन practical example है जो user authentication की basic concept को समझाता है। इस program में आप सीखेंगे कि कैसे username और password को input लेकर strcmp() function की मदद से compare किया जाता है।
इससे आपको string handling, conditional statements (if-else) और logical operators (&&) की बेहतर समझ मिलेगी। Beginner के लिए यह program बहुत फायदेमंद है क्योंकि यही concept आगे चलकर बड़े login systems और authentication projects में काम आता है।
Program Statement
एक ऐसा C Language Login Program बनाइये जिसमें पहले से defined username और password हो। User से username और password input लिया जाए और अगर दोनों सही हों तो “Login Successfully” message दिखे, अन्यथा “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: Correct Input
Enter User name : admin
Enter Password : 123456
Login Successfully
Case 2: Wrong Input
Enter User name : admin
Enter Password : 1111
Invalid Credential!
Code Explanation
1️. Header Files
#include<stdio.h>
#include<string.h>
- stdio.h → Input और Output functions (printf, scanf) के लिए
- string.h → strcmp() function use करने के लिए
2️. Variable Declaration
char user[20] = “admin”;
char password[20] = “123456”;
यहाँ predefined username और password set किया गया है।
char user1[20];
char password1[20];
इन variables में user से input लिया जाएगा।
3️. User Input
scanf(“%s”,user1);
scanf(“%s”,password1);
%s format specifier string input के लिए use होता है।
4️. Condition Check
if(strcmp(user,user1) == 0 && strcmp(password,password1) == 0)
- strcmp() दो strings को compare करता है।
- अगर दोनों strings same हों तो 0 return करता है।
- यहाँ logical AND (&&) operator use किया गया है ताकि username और password दोनों सही check किये जा सकें।
5️. Output
- Condition true → Login Successfully
- Condition false → Invalid Credential!
