Menu
Coddy logo textTech

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 icon

Challenge

Easy

Write a C program that demonstrates reading string input from the user using scanf and displaying it back. Your program should:

  1. Declare a character array named username with 30 elements to store the user's input
  2. Declare a character array named hobby with 25 elements to store another input
  3. Use printf to prompt the user with the message "Enter your username:"
  4. Use scanf with the %s format specifier to read the username into the username array
  5. Use printf to prompt the user with the message "Enter your hobby:"
  6. Use scanf with the %s format specifier to read the hobby into the hobby array
  7. Print both strings using printf in 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow