Unraveling the scanf() Function

One of the things I dread in my books, is foisting the scanf() function upon a beginner. I have no way around this frustration.

The scanf() function is useful in that it cleanly gathers input, converting it into a value. It’s horrid for reading strings, but the function is vital for basic input and getting a beginner started. My issue is that the format is straight from the fiery pits of Hell, darkly illustrated in Figure 1.

The scanf() function's arguments

Figure 1. How the scanf() function’s arguments are put together.

Here you go, eager beginner! Good luck! But let me explain what’s going on.

I believe beginners have a good handle on the function thing: They recognize that scanf() is a function. They can even stomach the two arguments, separated by a comma. But these are the most wily arguments possible: "%d" and &a.

The first mystery item is a format string. It uses cryptic placeholders to represent the type of data requested.

The second mystery item is the address of a variable, a concept relying upon a knowledge of pointers. No need to mention that pointers are the most terrifying aspect of C or any programming language, a topic postponed well into adulthood and completely out of place at the start of any training material. Yet here it is.

Like most cryptic things, scanf() is powerful. Its flexibility allows you to comb standard input, setting whatever text flows in to a given variable. This variable must be passed as a pointer argument; it would subtract from scanf()‘s usefulness to otherwise assign it a specific data type, say to instead return the input value. Therefore the function must be cryptic to a degree because the data type placeholder must match the variable’s data type, both of which must be flexible.

Beginners can tolerate scanf() as they learn, but I desire to dive deeper. Consider the format string. It’s pretty much the same as the printf() format string, so must the input match exactly?

2020_08_29_Lesson.c

#include <stdio.h>

int main()
{
    int a;

    printf("How old are you? ");
    scanf("I am %d years old",&a);
    printf("I'm %d, too!\n",a);
    
    return(0);
}

The scanf() format string at Line 8 contains some text in addition to the placeholder. This specific format restricts input only to text that matches.

For my first sample run, I just typed in a value:

How old are you? 29
I'm 266395685, too!

Oops. The format string demands more text input. Here’s my second sample run:

How old are you? I am 29 years old
I'm 29, too!

In this run, I typed the full text, I am 29 years old. This is the input scanf() is looking for. It translates the only value, but the rest of the text input is necessary for proper interpretation.

Next, I tried to mess with the function by typing only the first part of the format string:

How old are you? I am 29

years old
I'm 29, too!

After typing I am 29 I pressed Enter. Nothing. Enter again. Nothing. Only when I typed the rest of the format string did scanf() finally process the input.

This formatting string quirk isn’t the only oddity with scanf(). Another way to abuse this function is covered in next week’s Lesson.

Leave a Reply