String Input with scanf
Part of the Logic & Flow section of Coddy's C journey — lesson 14 of 63.
Now that you understand how strings work with the null terminator, it's time to learn how to get string input from users. The scanf function can read strings using the %s format specifier, but it comes with important limitations you need to understand.
To read a string with scanf, you use the %s format and pass the name of your character array (without the & operator, since array names are already addresses):
char word[50];
printf("Enter a word: ");
scanf("%s", word);
printf("You entered: %s\n", word);However, scanf("%s", ...) has a critical limitation: it stops reading as soon as it encounters any whitespace character (space, tab, or newline). This means if a user types "Hello World", scanf will only capture "Hello" and leave "World" in the input buffer.
Another serious concern is that scanf doesn't check if the input fits in your array. If someone enters a word longer than your array can hold, it will cause a buffer overflow, potentially crashing your program or creating security vulnerabilities. Always ensure your character array is large enough for the expected input.
Despite these limitations, scanf("%s", ...) is perfectly suitable for reading single words, which is exactly what you'll practice in the upcoming challenge.
Challenge
EasyWrite a C program that demonstrates reading string input from the user using scanf and displaying it back. Your program should:
- Declare a character array named
usernamewith 30 elements to store the user's input - Declare a character array named
hobbywith 25 elements to store another input - Use
printfto prompt the user with the message "Enter your username:" - Use
scanfwith the%sformat specifier to read the username into theusernamearray - Use
printfto prompt the user with the message "Enter your hobby:" - Use
scanfwith the%sformat specifier to read the hobby into thehobbyarray - Print both strings using
printfin the exact format shown below
The input will be provided as two separate single words (no spaces within each word).
Your output should display the results in the following format:
Enter your username:
Enter your hobby:
Hello [username], your hobby is [hobby]!For example, if the user enters "Alice" as the username and "reading" as the hobby, the output should be:
Enter your username:
Enter your hobby:
Hello Alice, your hobby is reading!This challenge tests your understanding of using scanf with the %s format specifier to read string input, proper character array declaration with sufficient size, and combining user input with formatted output using printf.
Cheat sheet
To read string input from users, use scanf with the %s format specifier:
char word[50];
printf("Enter a word: ");
scanf("%s", word);
printf("You entered: %s\n", word);Important limitations of scanf("%s", ...):
- Stops reading at any whitespace character (space, tab, newline)
- Only captures single words - "Hello World" becomes just "Hello"
- No buffer overflow protection - ensure your array is large enough
- Don't use the
&operator with array names (they're already addresses)
Try it yourself
#include <stdio.h>
int main() {
// TODO: Declare your character arrays here
// TODO: Write your code here to prompt and read input
// TODO: Print the final output message
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions