1SCANF(3S)                                                            SCANF(3S)
2
3
4

NAME

6       scanf, fscanf, sscanf - formatted input conversion
7

SYNOPSIS

9       #include <stdio.h>
10
11       scanf(format [ , pointer ] . . .  )
12       char *format;
13
14       fscanf(stream, format [ , pointer ] . . .  )
15       FILE *stream;
16       char *format;
17
18       sscanf(s, format [ , pointer ] . . .  )
19       char *s, *format;
20

DESCRIPTION

22       Scanf  reads  from  the standard input stream stdin.  Fscanf reads from
23       the named input stream.  Sscanf reads  from  the  character  string  s.
24       Each  function reads characters, interprets them according to a format,
25       and stores the results in its arguments.  Each expects as  arguments  a
26       control  string format, described below, and a set of pointer arguments
27       indicating where the converted input should be stored.
28
29       The control string usually contains  conversion  specifications,  which
30       are  used  to  direct  interpretation  of input sequences.  The control
31       string may contain:
32
33       1.  Blanks, tabs or newlines, which match optional white space  in  the
34           input.
35
36       2.  An  ordinary  character (not %) which must match the next character
37           of the input stream.
38
39       3.  Conversion  specifications,  consisting  of  the  character  %,  an
40           optional  assignment suppressing character *, an optional numerical
41           maximum field width, and a conversion character.
42
43       A conversion specification directs the conversion  of  the  next  input
44       field;  the  result  is placed in the variable pointed to by the corre‐
45       sponding argument, unless assignment suppression was  indicated  by  *.
46       An  input  field  is  defined  as  a string of non-space characters; it
47       extends to the next inappropriate character or until the  field  width,
48       if specified, is exhausted.
49
50       The  conversion  character  indicates  the  interpretation of the input
51       field;  the  corresponding  pointer  argument  must  usually  be  of  a
52       restricted type.  The following conversion characters are legal:
53
54       %   a  single `%' is expected in the input at this point; no assignment
55           is done.
56
57       d   a decimal integer is expected; the corresponding argument should be
58           an integer pointer.
59
60       o   an  octal integer is expected; the corresponding argument should be
61           a integer pointer.
62
63       x   a hexadecimal  integer  is  expected;  the  corresponding  argument
64           should be an integer pointer.
65
66       s   a  character  string is expected; the corresponding argument should
67           be a character pointer pointing to an  array  of  characters  large
68           enough  to  accept the string and a terminating `\0', which will be
69           added.  The input field is terminated by a  space  character  or  a
70           newline.
71
72       c   a  character  is  expected;  the corresponding argument should be a
73           character pointer.  The normal skip over space characters  is  sup‐
74           pressed  in  this  case;  to read the next non-space character, try
75           `%1s'.  If a field  width  is  given,  the  corresponding  argument
76           should  refer  to  a  character  array, and the indicated number of
77           characters is read.
78
79       e   a floating point number is expected; the next  field  is  converted
80       f   accordingly  and  stored  through the corresponding argument, which
81           should be a pointer to a float.   The  input  format  for  floating
82           point  numbers  is  an  optionally signed string of digits possibly
83           containing a decimal point, followed by an optional exponent  field
84           consisting of an E or e followed by an optionally signed integer.
85
86       [   indicates  a  string  not to be delimited by space characters.  The
87           left bracket is followed  by  a  set  of  characters  and  a  right
88           bracket;  the characters between the brackets define a set of char‐
89           acters making up the string.  If the first character is not circum‐
90           flex (^), the input field is all characters until the first charac‐
91           ter not in the set between the brackets;  if  the  first  character
92           after  the  left  bracket  is  ^, the input field is all characters
93           until the first character which is in the remaining set of  charac‐
94           ters  between  the brackets.  The corresponding argument must point
95           to a character array.
96
97       The conversion characters d, o and x may be capitalized or preceded  by
98       l to indicate that a pointer to long rather than to int is in the argu‐
99       ment list.  Similarly, the conversion characters e or f may be capital‐
100       ized  or  preceded  by l to indicate a pointer to double rather than to
101       float.  The conversion characters d, o and x may be preceded  by  h  to
102       indicate a pointer to short rather than to int.
103
104       The  scanf  functions  return  the  number  of successfully matched and
105       assigned input items.  This can be used to decide how many input  items
106       were  found.  The constant EOF is returned upon end of input; note that
107       this is different from 0, which means that no conversion was  done;  if
108       conversion  was intended, it was frustrated by an inappropriate charac‐
109       ter in the input.
110
111       For example, the call
112
113                 int i; float x; char name[50];
114                 scanf("%d%f%s", &i, &x, name);
115
116       with the input line
117
118              25   54.32E−1  thompson
119
120       will assign to i the value 25, x the value 5.432, and name will contain
121       `thompson\0'.  Or,
122
123              int i; float x; char name[50];
124              scanf("%2d%f%*d%[1234567890]", &i, &x, name);
125
126       with input
127
128              56789 0123 56a72
129
130       will  assign  56  to  i,  789.0 to x, skip `0123', and place the string
131       `56\0' in name.  The next call to getchar will return `a'.
132

SEE ALSO

134       atof(3), getc(3S), printf(3S)
135

DIAGNOSTICS

137       The scanf functions return EOF on end of input, and a short  count  for
138       missing or illegal data items.
139

BUGS

141       The  success  of  literal  matches  and  suppressed  assignments is not
142       directly determinable.
143
144
145
1467th Edition                      May 15, 1985                        SCANF(3S)
Impressum