O'Reilly logo

C in a Nutshell by Tony Crawford, Peter Prinz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

scanf

Synopsis

Reads formatted data from standard input

#include <stdio.h>
intscanf( const char * restrict format, ... );

The scanf() function reads a sequence of characters from the standard input stream and parses it for the data items specified by the format string. The function then stores the data in the locations addressed by the subsequent pointer arguments.

The ellipsis (...) in the function prototype indicates that scanf() takes a variable number of optional arguments. All parameters after those explicitly declared can be considered to be of the type void *, which means that you can pass any type of object pointer to scanf() in that position. Each of these pointer arguments must point to a variable whose type agrees with the corresponding conversion specification in the format string. If there are more such arguments than conversion specifiers, the excess arguments are ignored.

Conversion specification syntax

For a general overview of data conversion with scanf(), see "Formatted Input" in Chapter 13. This section describes the syntax of conversion specifications in the scanf() format string in detail. The conversion specifications have the following syntax:

%[*][field width][length modifier]specifier

Before processing each conversion specification in the format string, scanf() skips over any whitespace characters in the input stream (except with the conversion specifiers c and [ ], which we will describe in a moment). For each conversion specification, scanf() reads one or ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required