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 ...

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.