C User Input
User Input
C user input is an essential concept for creating interactive programs that can accept data from the user.
To get user input, you can use the scanf() function:Example
Output a number entered by the user:
#include <stdio.h>
int main() {
// Create an integer variable that will store the number we get from the user
int myNum;
// Ask the user to type a number
printf("Type a number and press enter: \n");
// Get and save the number the user types
scanf("%d", &myNum);
// Print the number the user typed
printf("Your number is: %d", myNum);
return 0;
}
Output
Type a number and press enter: 7
Your number is: 7
The scanf() function takes two arguments: the variable format specifier (%d in the above example) and the reference operator (& myNum), which stores the memory address of the variable.
Tip: You will learn more about memory addresses and functions in the next chapter.
Multiple Inputs
The scanf() function in C programming does allow you to read multiple inputs in a single line, provided you separate them using appropriate format specifiers:
Example
#include <stdio.h>
int main() {
// Create an int and a char variable
int myNum;
char myChar;
// Ask the user to type a number AND a character
printf("Type a number AND a character and press enter: \n");
// Get and save the number AND character the user types
scanf("%d %c", &myNum, &myChar);
// Print the number
printf("Your number is: %d\n", myNum);
// Print the character
printf("Your character is: %c\n", myChar);
return 0;
}
Output
Type a number AND a character and press enter: 5b
Your number is: 5
Your character is: b
Take String Input
You can also get a string entered by the user:
Example
#include <stdio.h>
int main() {
// Create a string
char firstName[30];
// Ask the user to input some text (name)
printf("Enter your first name and press enter: \n");
// Get and save the text
scanf("%s", firstName);
// Output the text
printf("Hello %s", firstName);
return 0;
}
Output
Enter your first name and press enter:
CodeLines
Hello CodeLines
Note: While the statement about specifying the size of the string array in scanf is partially true, there are some nuances and inaccuracies to consider:
scanf() considers whitespace (spaces, tabs, newlines) as a delimiter when reading input. This means when you type multiple words, it only reads the first word until it encounters a whitespace character.
Example
char fullName[30];
printf("Type your full name: \n");
scanf("%s", &fullName);
printf("Hello %s", fullName);
// Type your full name: John Doe
// Hello John
From the above example, you would expect the program to print "Hello Ram", but it only prints "Hello".".
fgets() is commonly used to read lines of text from a file stream or the standard input (stdin). It allows you to specify the maximum number of characters to read, including the newline character (\n), which is then included in the retrieved string.
Example
#include <stdio.h>
int main() {
// Create a string
char fullName[30];
// Ask the user to input some text (full name)
printf("Type your full name and press enter: \n");
// Get the text
fgets(fullName, sizeof(fullName), stdin);
// Output the text
printf("Hello %s", fullName);
return 0;
}
Output
Type your full name and press enter:
Hello CodeLines
Hello Hello CodeLines
Use the scanf() function to get a single word as input, and use fgets() for multiple words.