Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
fscanf() |
Input formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
fscanf_s() |
Input formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
fwscanf() |
Input formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
fwscanf_s() |
Input formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
scanf() |
Input formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
scanf_s() |
Input formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
sscanf() |
Input formatted text | (·) | <stdio.h> |
C89 | C90 | C95 | C99 | C11 | |||
sscanf_s() |
Input formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
swscanf() |
Input formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
swscanf_s() |
Input formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vfscanf() |
Input formatted text | (·) | <stdio.h> |
C99 | C11 | ||||||
vfscanf_s() |
Input formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vfwscanf() |
Input formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
vfwscanf_s() |
Input formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vscanf() |
Input formatted text | (·) | <stdio.h> |
C99 | C11 | ||||||
vscanf_s() |
Input formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vsscanf() |
Input formatted text | (·) | <stdio.h> |
C99 | C11 | ||||||
vsscanf_s() |
Input formatted text | ? | (·) | <stdio.h> |
C11 | ||||||
vswscanf() |
Input formatted text | (·) | <wchar.h> |
C99 | C11 | ||||||
vswscanf_s() |
Input formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
vwscanf() |
Input formatted text | (·) | <wchar.h> |
C99 | C11 | ||||||
vwscanf_s() |
Input formatted text | ? | (·) | <wchar.h> |
C11 | ||||||
wscanf() |
Input formatted text | (·) | <wchar.h> |
C95 | C99 | C11 | |||||
wscanf_s() |
Input formatted text | ? | (·) | <wchar.h> |
C11 |
C provides a family of functions for parsing character sequences as strings and numbers:
#include<stdio.h>
int scanf(const char *fmt, ...); int fscanf(FILE *in, const char *fmt, ...); int sscanf(const char *buf, const char *fmt, ...); #include<stdarg.h>
int vscanf(const char *fmt, va_list va); int vfscanf(FILE *in, const char *fmt, va_list va); int vsscanf(const char *buf, const char *fmt, va_list va);
#include<wchar.h>
int wscanf(const wchar_t *fmt, ...); int swscanf(const wchar_t *buf, const wchar_t *fmt, ...); #include<stdarg.h>
int vwscanf(const wchar_t *fmt, va_list va); int vswscanf(const wchar_t *buf, const wchar_t *fmt, va_list va);
#include<stdio.h>
#include<wchar.h>
int fwscanf(FILE *in, const wchar_t *fmt, ...); #include<stdarg.h>
int vfwscanf(FILE *in, const wchar_t *fmt, va_list va);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <stdio.h>
int scanf_s(const char *fmt, ...); int fscanf_s(FILE *in, const char *fmt, ...); int sscanf_s(const char *buf, const char *fmt, ...); #include<stdarg.h>
int vscanf_s(const char *fmt, va_list va); int vfscanf_s(FILE *in, const char *fmt, va_list va); int vsscanf_s(const char *buf, const char *fmt, va_list va);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <wchar.h>
int wscanf_s(const wchar_t *fmt, ...); int swscanf_s(const wchar_t *buf, const wchar_t *fmt, ...); #include<stdarg.h>
int vwscanf_s(const wchar_t *fmt, va_list va); int vswscanf_s(const wchar_t *buf, const wchar_t *fmt, va_list va);
#define __STDC_WANT_ 1 #includeLIB_ EXT1__ <stdio.h>
#include<wchar.h>
int fwscanf_s(FILE *in, const wchar_t *fmt, ...); #include<stdarg.h>
int vfwscanf_s(FILE *in, const wchar_t *fmt, va_list va);
Each function takes a format string fmt
and a variable number of pointer arguments in
one form or another, which are consumed in sequence according
to the format string. It also has a defined input source,
which may be an open stream or a character array. Each
function consumes characters from the source, and matches
them in succession against characters in the format string.
It stops when it fails to match the source against the format
string, leaving the failed characters in the source. The
number of converted and assigned arguments is returned, or
-1 on error.
The format string may contain substrings called conversion specifiers, which match sets of patterns according to the parameters contained within the specifier. If the next characters in the source match one of the patterns, they are consumed from the source, and converted into a value. An argument is also consumed, and interpreted as a pointer to a variable, then the converted value is assigned to the variable. The number of successful assignments is returned.
One or more adjacent white-space characters in the format string match and consume one or more white-space characters in the source. Other characters that are not part of conversion specifiers must match the source exactly.
Each conversion specifier begins with %
(U+0025), and
continues with an optional flag *, then an
optional integer field width, then an optional type modifier
(one of "hh"
, "h"
, "l"
, "ll"
, "j"
, "z"
, "t"
and "L"
), and finally a format code (one of
"d"
, "i"
,
"u"
, "o"
,
"x"
, "X"
,
"a"
, "A"
,
"e"
, "E"
,
"f"
, "F"
,
"g"
, "G"
,
"c"
, "s"
,
"p"
and "n"
).
Format code | Format | Argument type for each type modifier | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
hh | h | None | l | ll | j | z | t | L | |||
From C99 | From C99 | ||||||||||
d | Signed base-10 integer | signed char * |
short
* |
int * |
long * |
long long * |
intmax_t * |
ptrdiff_t * |
N/A | ||
i | Signed base-8, -10 or -16 integer, depending on 0/0x prefix | ||||||||||
u | Unsigned base-10 integer | unsigned char * |
unsigned short
* |
unsigned
* |
unsigned long * |
unsigned long long * |
uintmax_t * |
size_t * |
|||
o | Unsigned base-8 integer | ||||||||||
x | Unsigned base-16 integer | ||||||||||
X | |||||||||||
a | From C99 | Real, in accordance with strtof |
N/A | float
* |
double
* |
N/A | N/A | long double
* |
|||
A | |||||||||||
e | |||||||||||
E | |||||||||||
f | |||||||||||
F | |||||||||||
g | |||||||||||
G | |||||||||||
c | Characters | char * |
wchar_t * |
N/A | |||||||
s | Non-white-space string | ||||||||||
[chars] | String consisting of chars | ||||||||||
[^chars] | String not consisting of chars | ||||||||||
p | Pointer | void
** |
N/A | ||||||||
n | None; store count of conversions performed so far | signed
char
* |
short
* |
int * |
long * |
long long * |
|||||
% | Literal % |
N/A |
Only lower-case letters might acquire new meanings in conversion specifiers.
The * flag prevents an argument from being consumed, so no assignment takes place. The field width limits the number of source characters that may be consumed to make the conversion.
These functions return EOF
if an error occurs before the
first conversion is completed.
Twenty-four functions exist because C does not support any name overloading, yet there are many independent options that must be selected. The form of the name is {v}{f,s}{w}scanf{_s}.
-
The first part v indicates that the argument list is supplied as a
va_
given as the last argument. Without v, the function is declared withlist ...
at the end of its parameter list, so arguments are given directly. -
The second part f,s,sn selects the input.
- s indicates that characters are to be
read from an array, until a null terminator. The first
argument
buf
is a pointer to the first element to be read from. -
f indicates that characters are to be
read from a stream,
FILE *fp
, which is given as the first argument. The function stops reading at end-of-file. - Functions with none of these prefixes read from the
standard input stream,
stdin
.
- s indicates that characters are to be
read from an array, until a null terminator. The first
argument
-
The third part w indicates that the function operates on wide characters. The format string is a null-terminated array of
wchar_t
. s functions read from arrays ofwchar_t
. f functions are wide-oriented.Without w, the function operates on multibyte characters. The format string is a null-terminated array of
char
. s functions read from arrays ofchar
. f functions are byte-oriented. -
_s functions apply additional bounds checking, and fail on other constraint violations:
- %n as a conversion specifier;
NULL
as an argument corresponding to any conversion specifier.