In C, user input is typically gathered using functions like scanf()
and gets()
. These functions allow programs to accept data from users during execution, making the code interactive and dynamic.
Basic User Input with scanf()
scanf
is the most commonly used function to read formatted input from the user. It can handle multiple data types such as integers, floats, and characters.
Example 1: Reading an Integer
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
return 0;
}
Output:
Enter a number: 25
You entered: 25
How It Works
%d
specifies that the input will be an integer.&number
is the address of the variable where the input will be stored.
Example 2: Reading Multiple Inputs
#include <stdio.h>
int main() {
int age;
float height;
printf("Enter your age and height: ");
scanf("%d %f", &age, &height);
printf("You are %d years old and %.2f meters tall.\n", age, height);
return 0;
}
Output:
Enter your age and height: 21 5.8
You are 21 years old and 5.80 meters tall.
Explanation
%f
reads float values.- The input must be entered in the specified format, separating the values with space.
Example 3: Reading a Character
#include <stdio.h>
int main() {
char grade;
printf("Enter your grade: ");
scanf(" %c", &grade); // Space before %c to avoid newline issues
printf("Your grade is: %c\n", grade);
return 0;
}
Output:
Enter your grade: A
Your grade is: A
Reading Strings
Example 4: Reading a String
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
Output:
Enter your name: Sunil
Hello, Sunil!
Handling Spaces in Strings with gets()
scanf
cannot handle strings with spaces. To overcome this, gets()
or fgets()
is used.
Example 5: Reading Full Names
#include <stdio.h>
int main() {
char fullName[100];
printf("Enter your full name: ");
fgets(fullName, sizeof(fullName), stdin);
printf("Hello, %s", fullName);
return 0;
}
Output:
Enter your full name: Sunil Kumar
Hello, Sunil Kumar
Real-Life Example: Login System
#include <stdio.h>
#include <string.h>
int main() {
char username[20], password[20];
char correct_username[] = "admin";
char correct_password[] = "1234";
printf("Enter Username: ");
scanf("%s", username);
printf("Enter Password: ");
scanf("%s", password);
if (strcmp(username, correct_username) == 0 && strcmp(password, correct_password) == 0) {
printf("Login Successful!\n");
} else {
printf("Invalid Credentials.\n");
}
return 0;
}
Explanation
strcmp()
compares the input with stored values.- The program checks if the username and password match the stored values.
Conclusion
User input is crucial for interactive programs in C. Functions like scanf
and fgets
provide efficient ways to accept different data types. Mastering these functions enhances your ability to create dynamic and user-driven applications.
For more coding insights and practical examples, visit Master Coding Science at mastercodingscience.com.