summaryrefslogtreecommitdiffstats
path: root/man3/scanf.3
blob: 79345095c1e67b9c01e823e984703db470377d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'\" t
.\" Copyright 2022 Alejandro Colomar <alx@kernel.org>
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH scanf 3 2023-07-20 "Linux man-pages 6.05.01"
.SH NAME
scanf, fscanf, vscanf, vfscanf \- input FILE format conversion
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <stdio.h>
.PP
.BI "int scanf(const char *restrict " format ", ...);"
.BI "int fscanf(FILE *restrict " stream ,
.BI "           const char *restrict " format ", ...);"
.PP
.B #include <stdarg.h>
.PP
.BI "int vscanf(const char *restrict " format ", va_list " ap );
.BI "int vfscanf(FILE *restrict " stream ,
.BI "           const char *restrict " format ", va_list " ap );
.fi
.PP
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.PP
.BR vscanf (),
.BR vfscanf ():
.nf
    _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
.fi
.SH DESCRIPTION
The
.BR scanf ()
family of functions scans input like
.BR sscanf (3),
but read from a
.IR FILE .
It is very difficult to use these functions correctly,
and it is preferable to read entire lines with
.BR fgets (3)
or
.BR getline (3)
and parse them later with
.BR sscanf (3)
or more specialized functions such as
.BR strtol (3).
.PP
The
.BR scanf ()
function reads input from the standard input stream
.I stdin
and
.BR fscanf ()
reads input from the stream pointer
.IR stream .
.PP
The
.BR vfscanf ()
function is analogous to
.BR vfprintf (3)
and reads input from the stream pointer
.I stream
using a variable argument list of pointers (see
.BR stdarg (3).
The
.BR vscanf ()
function is analogous to
.BR vprintf (3)
and reads from the standard input.
.SH RETURN VALUE
On success, these functions return the number of input items
successfully matched and assigned;
this can be fewer than provided for,
or even zero, in the event of an early matching failure.
.PP
The value
.B EOF
is returned if the end of input is reached before either the first
successful conversion or a matching failure occurs.
.B EOF
is also returned if a read error occurs,
in which case the error indicator for the stream (see
.BR ferror (3))
is set, and
.I errno
is set to indicate the error.
.SH ERRORS
.TP
.B EAGAIN
The file descriptor underlying
.I stream
is marked nonblocking, and the read operation would block.
.TP
.B EBADF
The file descriptor underlying
.I stream
is invalid, or not open for reading.
.TP
.B EILSEQ
Input byte sequence does not form a valid character.
.TP
.B EINTR
The read operation was interrupted by a signal; see
.BR signal (7).
.TP
.B EINVAL
Not enough arguments; or
.I format
is NULL.
.TP
.B ENOMEM
Out of memory.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR scanf (),
.BR fscanf (),
.BR vscanf (),
.BR vfscanf ()
T}	Thread safety	MT-Safe locale
.TE
.sp 1
.SH STANDARDS
C11, POSIX.1-2008.
.SH HISTORY
C99, POSIX.1-2001.
.SH SEE ALSO
.BR fgets (3),
.BR getline (3),
.BR sscanf (3)