A scanf() String Trick

I’m not a fan of the scanf() function, despite how handy it can be. It’s a great teaching tool, but for a program that accepts string input, you need to use fgets() instead. Still, scanf() can be used to read a string, providing that you know the whitespace requirements beforehand.

As a review, the scanf() function fetches specific type of information from standard input. The format for desired input is specified as the function’s first argument, a string. That string typically contains a single placeholder character, such as %d for an int value.

After the formatting string, scanf()‘s second argument is pointer variable; the & operator is prefixed to the variable if it’s not already a pointer. The goal is to pass the variable’s address to the function so that the value fetched from standard input is stored in that variable.

Here’s a sample:

scanf("%c",&ch);

A single character, represented by %c, is fetched from the standard input stream and saved into char variable ch.

What frustrates me about scanf() and strings is the function’s fixation with whitespace characters: At the sign of the first space, tab, or newline character, the function stops reading input. So scanf() isn’t a string-input function; it’s more of a word-input function.

You can take advantage of scanf()‘s word-input aspect to read multiple words in a string, but only when you’re certain of the exact number of words.

As an example, the following program asks the user to type in their first, middle, and last names — three character chunks separated by whitespace:

#include <stdio.h>

int main()
{
    char first[24], middle[24], last[24];

    printf("Type your name (first middle last): ");
    scanf("%23s",first);
    scanf("%23s",middle);
    scanf("%23s",last);
    printf("Glad to meet you, %s %s %s\n",
            first,
            middle,
            last);

    return(0);
}

I set the buffer size to 24 characters for each of the char arrays (strings). Further, the formatting string for the scanf() functions uses 23 to help truncate input. (The 24th character is the null, \0, at the end of the string.) Here’s a sample run:

Type your name (first middle last): Arthur Templeton Grockmeister
Glad to meet you, Arthur Templeton Grockmeister

The three scanf() functions read each work chunk from the input stream: The scanf() function at Line 8 stops reading after the first space. The next scanf() function at Line 9 stops after the second space. The final scanf() function (Line 10) stops at the newline character.

If you type a name with more than 23 characters, input is cropped, thanks to the %23s string format placeholder:

Type your name (first middle last): Art T Grockalockfickafockameister
Glad to meet you, Art T Grockalockfickafockamei

If the user types a fewer than three words, then the program continues to wait until all three scanf() functions are fed. That’s not good program design, of course, which is why I instead prefer a single string-swallowing function like fgets().

Leave a Reply