-
Hajipur, Bihar, 844101
In C programming, user input is an essential aspect of interactive programs. Without the ability to take input from users, programs would be static and only able to work with hardcoded values. By accepting input, your programs can adapt dynamically, perform calculations, make decisions, or store information for later use.
C does not have built-in input objects or methods like some modern languages. Instead, it relies on functions from the standard I/O library <stdio.h> to read data from the keyboard. The most commonly used functions for user input are scanf(), getchar(), and fgets(). Understanding these functions is crucial for building robust, interactive programs.
The scanf() function is the most widely used function in C for reading formatted input. It allows the programmer to specify the type of data expected, such as integers, floating-point numbers, characters, or strings.
Syntax:
scanf("format_specifier", &variable);
format_specifier: Indicates the type of input. Common specifiers include %d (integer), %f (float), %c (character), and %s (string).
&variable: The address-of operator (&) tells scanf() where to store the input value.
Example: Reading an Integer
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}
Here, %d matches the integer type of age, and &age provides the memory location for storage.
Always use & for variables except when reading strings.
scanf() can read multiple values in one line, which makes programs concise:
#include <stdio.h>
int main() {
int a;
float b;
char c;
printf("Enter an integer, a float, and a character: ");
scanf("%d %f %c", &a, &b, &c);
printf("Integer: %d\nFloat: %.2f\nCharacter: %c\n", a, b, c);
return 0;
}
Inputs can be separated by spaces, tabs, or newlines.
The order and type of inputs must match the format specifiers exactly.
Strings are handled differently because they are arrays of characters. Using scanf() for strings reads until the first whitespace:
#include <stdio.h>
int main() {
char name[50];
printf("Enter your first name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
Limitation: Stops at spaces. For full names or sentences, fgets() is preferred.
The fgets() function reads an entire line including spaces:
#include <stdio.h>
int main() {
char fullname[100];
printf("Enter your full name: ");
fgets(fullname, sizeof(fullname), stdin);
printf("Hello, %s", fullname);
return 0;
}
sizeof(fullname) prevents buffer overflow.
fgets() includes the newline character, which can be removed with string manipulation if needed.
For single-character input, getchar() is useful:
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: %c\n", ch);
return 0;
}
Commonly used for menu selection or capturing yes/no responses.
It is possible to combine different input functions for more complex scenarios:
#include <stdio.h>
int main() {
int age;
float height;
char name[50];
printf("Enter age and height: ");
scanf("%d %f", &age, &height);
getchar(); // Clear leftover newline
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin);
printf("Name: %sAge: %d\nHeight: %.2f\n", name, age, height);
return 0;
}
Here, getchar() clears the newline left in the input buffer after scanf().
Mixing scanf() and fgets() allows reading both numbers and full strings safely.
User input may not always match expectations. Input validation is critical:
#include <stdio.h>
int main() {
int age;
printf("Enter your age (1-120): ");
scanf("%d", &age);
if(age < 1 || age > 120) {
printf("Invalid age entered.\n");
} else {
printf("You entered: %d\n", age);
}
return 0;
}
This prevents invalid or nonsensical input.
Proper validation ensures programs are reliable and safe.
Forgetting the & operator for non-string variables.
Using scanf() for strings with spaces—stops at the first space.
Not clearing the input buffer when switching between numbers and strings.
Assuming users will always input the correct data type.
Not limiting input size, which can lead to buffer overflow.
#include <stdio.h>
int main() {
char username[30];
int age;
printf("Enter your username: ");
fgets(username, sizeof(username), stdin);
printf("Enter your age: ");
scanf("%d", &age);
printf("Registration complete!\nUsername: %sAge: %d\n", username, age);
return 0;
}
Demonstrates combining strings and numbers.
Ensures safe input by specifying the array size for strings.
User input in C is handled using scanf(), getchar(), and fgets(), each suitable for different scenarios. Understanding their strengths, limitations, and proper usage ensures programs are interactive, reliable, and user-friendly. Input validation, buffer management, and careful choice of input functions are key to building robust C applications.
Write a program to read an integer from the user and print whether it is even or odd.
Create a program to read a float representing the user’s height and print it with two decimal places.
Write a program to read a single character and check if it is a vowel or consonant.
Develop a program to read a string (first name) and print it in uppercase.
Write a program to read an integer, float, and character in one line and display them.
Create a program to read a full name (including spaces) using fgets() and greet the user.
Write a program to take age as input and validate it to be between 1 and 120.
Develop a program that asks the user for two numbers and prints their sum, difference, product, and quotient.
Write a program to read a string and count the number of vowels in it.
Create a program to read a line of text using fgets(), then print each word on a separate line.