Introduction
In this tutorial, we will create a simple login and registration system using the C programming language. This system will allow users to register an account with a username and password, and then log in with their credentials. Basic authentication will be implemented to ensure secure user interaction.
Objective
The primary objective of this program is to demonstrate how to build a basic user authentication system. Users will be able to:
- Register with a unique username and password
- Login using their credentials
- Receive appropriate feedback for successful login or registration
Code Implementation
#include #include #define MAX_USERS 10 #define USERNAME_LEN 20 #define PASSWORD_LEN 20 struct User { char username[USERNAME_LEN]; char password[PASSWORD_LEN]; }; struct User users[MAX_USERS]; int user_count = 0; int register_user(char username[], char password[]) { if (user_count >= MAX_USERS) { printf("User limit reached! Cannot register more users.\n"); return 0; } for (int i = 0; i < user_count; i++) { if (strcmp(users[i].username, username) == 0) { printf("Username already exists! Please choose a different username.\n"); return 0; } } strcpy(users[user_count].username, username); strcpy(users[user_count].password, password); user_count++; printf("User registered successfully!\n"); return 1; } int login_user(char username[], char password[]) { for (int i = 0; i < user_count; i++) { if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) { return 1; // Successful login } } return 0; // Invalid credentials } int main() { int choice; char username[USERNAME_LEN]; char password[PASSWORD_LEN]; while (1) { printf("\n--- Menu ---\n"); printf("1. Register\n"); printf("2. Login\n"); printf("3. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); getchar(); // Consume the newline character left by scanf switch (choice) { case 1: printf("Enter username: "); fgets(username, sizeof(username), stdin); username[strcspn(username, "\n")] = 0; // Remove newline character printf("Enter password: "); fgets(password, sizeof(password), stdin); password[strcspn(password, "\n")] = 0; // Remove newline character register_user(username, password); break; case 2: printf("Enter username: "); fgets(username, sizeof(username), stdin); username[strcspn(username, "\n")] = 0; // Remove newline character printf("Enter password: "); fgets(password, sizeof(password), stdin); password[strcspn(password, "\n")] = 0; // Remove newline character if (login_user(username, password)) { printf("Login successful!\n"); } else { printf("Invalid username or password.\n"); } break; case 3: printf("Exiting the program.\n"); return 0; default: printf("Invalid choice. Please try again.\n"); } } return 0; }
Explanation of Program Structure
The program consists of several key parts:
- Structure Definition: We define a
struct User
that contains a username and password field to represent each user’s data. - Register Function: The
register_user
function checks if the username is unique and adds the new user to the system if available space exists. - Login Function: The
login_user
function compares the inputted username and password with stored records to validate login credentials. - Main Function: This is the entry point of the program, where the menu-driven interface allows the user to register, log in, or exit the system.
The program uses a simple menu system where the user can choose to either register, log in, or exit. The program runs in a loop until the user decides to exit by selecting the option “3”.
How to Run the Program
To run the program:
- Save the code to a file with a .c extension (e.g.,
auth_system.c
). - Compile the program using a C compiler such as GCC. Example:
gcc auth_system.c -o auth_system
. - Run the executable file created during the compilation. Example:
./auth_system
. - Follow the on-screen instructions to register a new user or log in with an existing account.