C - User Input: Handling Data from Users
User input is a crucial aspect of computer applications, enabling data collection for processing and output. In C, while there are no dedicated keywords for reading user inputs, the scanf()
function from the stdio.h
library is commonly employed to capture data. This section explores how to effectively implement user input in C programs.
C - User Input
Every computer application accepts data from the user, processes it, and produces output. There are no keywords in C to read user inputs. Instead, the stdio.h library function scanf() is commonly used for this purpose.
Example: User Input in C
Code Snippet: C
#include
int main() {
int price, qty, ttl;
printf("Enter price and quantity: ");
scanf("%d %d", &price, &qty);
ttl = price * qty;
printf("Total: %d", ttl);
return 0;
}
Output
Enter price and quantity: 150 10
Total: 1500
Integer Input
Use the %d format specifier for signed integers. Example:
Code Snippet: C
#include
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
Output
Enter an integer: 234
You entered: 234
Multiple Integer Inputs
Code Snippet: C
#include
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("You entered: %d and %d", num1, num2);
return 0;
}
Output
Enter two integers: 45 57
You entered: 45 and 57
Float Input
Use the %f format specifier for floating-point numbers. Example:
Code Snippet: C
#include
int main() {
float num;
printf("Enter a number: ");
scanf("%f", &num);
printf("You entered: %f", num);
return 0;
}
Output
Enter a number: 34.56
You entered: 34.560001
Integer and Float Inputs
Code Snippet: C
#include
int main() {
int num1;
float num2;
printf("Enter an integer and a float: ");
scanf("%d %f", &num1, &num2);
printf("You entered: %d and %.2f", num1, num2);
return 0;
}
Output
Enter an integer and a float: 65 34.5678
You entered: 65 and 34.57
Character Input
Use the %c format specifier for single characters. Add a space before %c in the format string. Example:
Code Snippet: C
#include
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
printf("You entered: %c", ch);
return 0;
}
Output
Enter a character: x
You entered: x
Multiple Character Inputs
Code Snippet: C
#include
int main() {
char ch1, ch2;
printf("Enter two characters: ");
scanf("%c %c", &ch1, &ch2);
printf("You entered: %c and %c", ch1, ch2);
return 0;
}
Output
Enter two characters: x y
You entered: x and y
String Input Using scanf()
Code Snippet: C
#include
int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("You entered: %s", name);
return 0;
}
Output
Enter your name: Ravikant
You entered: Ravikant
String Input Using gets()
Code Snippet: C
#include
int main() {
char name[20];
printf("Enter your name: ");
gets(name);
printf("You entered: %s", name);
return 0;
}
Output
Enter your name: Ravikant Soni
You entered: Ravikant Soni
Reading a Character Sequence
Code Snippet: C
#include
int main() {
char ch;
char word[10];
int i = 0;
printf("Enter characters. End by pressing the Enter key: ");
while(1) {
ch = getchar();
word[i] = ch;
if (ch == '\n')
break;
i++;
}
printf("\nYou entered: %s", word);
return 0;
}
Output
Enter characters. End by pressing the Enter key: Hello
You entered: Hello
Character Input Using getchar()
Code Snippet: C
#include
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
puts("You entered: ");
putchar(ch);
printf("\nYou entered: %c", ch);
return 0;
}
Output
Enter a character: W
You entered:
W
You entered: W