scanf (Glossary Entry)

One of the C standard library functions for reading and converting strings (sequences of ASCII characters) into other strings or numbers. It is often used to read a keyboard, or data from a serial interface. This is a complex function that can be difficult to master, made more difficult when input comes from a terminal (where different standards exist).

“The function scanf is the input analog of printf, providing many of the same conversion facilities in the opposite direction.

int scanf(char *format, …)

scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments.”

Excerpt From: Brian W. Kernighan & Dennis M. Ritchie. “C Programming Language.” 

The format parameter is a string containing formatting and conversion information, and … indicates a variable number of parameters (see below). 

See “TABLE 7-2. BASIC SCANF CONVERSIONS” and “7.3 Variable-length Argument Lists: in Brian W. Kernighan & Dennis M. Ritchie. “C Programming Language.”

The type char* is a pointer (address) to a list of characters in memory. This list (known as an array) must be terminated with the 8-bit value 0. In C, a short cut is to write the characters “inside double quotes”.

Some examples are given below:

//Read a string
char inputString[10]; //Room for 9 characters + \0
int hits;
int u;

printf("Type in an integer\n");
scanf("%d", &u);
printf("You entered %d\n", u);
printf("One more is %d\n", u+1);

The above is one of the simplest examples. The input string (typically from a keyboard / serial interface, but not necessarily) must be an integer number written with ASCII characters (human readable). The format string %d looks for an string representation of an integer, converts it to an actual integer value and overwrites the variable u. Not that the address &u is provided so that it can be modified in place.

printf("Enter the words: age=<number>, where <number> is an integer of your choice. No spaces!\n");
hits = scanf("age=%d", &u);
if (hits == 1) {
   printf("Hello, I understand you are %d years old\n", u);
} else {
   printf("You did not do exactly what I asked\n");
}

This example matches a specific pattern. The string “age=” followed immediately by an integer value. If this pattern is found, the scanf function returns the value 1.

printf("Type in a string wihtout spaces and press return\n");
scanf("%9s", inputString);
printf("You typed %s\n", inputString);

//Here are the ASCII characters
for (int n=0; n<10; n++) {
   printf("character %d = %d\n", n, inputString[n]);
}

Here scanf is used to read a string of 9 characters length. Note that a C string always has a 0 on the end, so the destination must have 10 bytes or more space. Note that there is no & in front of inputString. This is because inputString is an array, and giving its name returns the address of the first element.

printf("Enter a name and an age, with a space between\n");
scanf("%s %d", inputString, &u);
printf("Hello %s, I understand you are %d years old\n", inputString, u);
Here scanf is using multiple format characters. The white-space is ignored.

See Kernigan and Richie, Section 7.4 “Formatted Input – Scanf” for more details.