Namespaces
Variants
Actions

scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s

From cppreference.com
< c‎ | io
 
 
File input/output
Types and objects
        
Functions
File access
(C95)
Unformatted input/output
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)

Formatted input
scanffscanfsscanfscanf_sfscanf_ssscanf_s
(C11)(C11)(C11)
 
Defined in header <stdio.h>
(1)
int scanf( const char          *format, ... );
(until C99)
int scanf( const char *restrict format, ... );
(since C99)
(2)
int fscanf( FILE          *stream, const char          *format, ... );
(until C99)
int fscanf( FILE *restrict stream, const char *restrict format, ... );
(since C99)
(3)
int sscanf( const char          *buffer, const char          *format, ... );
(until C99)
int sscanf( const char *restrict buffer, const char *restrict format, ... );
(since C99)
int scanf_s(const char *restrict format, ...);
(4) (since C11)
int fscanf_s(FILE *restrict stream, const char *restrict format, ...);
(5) (since C11)
int sscanf_s(const char *restrict buffer, const char *restrict format, ...);
(6) (since C11)

Reads data from a variety of sources, interprets it according to format and stores the results into given locations.

1) reads the data from stdin
2) reads the data from file stream stream
3) reads the data from null-terminated character string buffer. Reaching the end of the string is equivalent to reaching the end-of-file condition for fscanf
4-6) Same as (1-3), except that %c, %s, and %[ conversion specifiers each expect two arguments (the usual pointer and a value of type rsize_t indicating the size of the receiving array, which may be 1 when reading with a %c into a single char) and except that the following errors are detected at runtime and call the currently installed constraint handler function:
  • any of the arguments of pointer type is a null pointer
  • format, stream, or buffer is a null pointer
  • the number of characters that would be written by %c, %s, or %[, plus the terminating null character, would exceed the second (rsize_t) argument provided for each of those conversion specifiers
  • optionally, any other detectable error, such as unknown conversion specifier
As with all bounds-checked functions, scanf_s, fscanf_s, and sscanf_s are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.

Contents