[0m


SCANF(3S)	    UNIX Programmer's Manual		SCANF(3S)



[1mNAME
     [0mscanf, fscanf, sscanf - formatted input conversion

[1mSYNOPSIS
     #include <stdio.h>

     scanf(format [0m[ , pointer ] . . .  [1m)
     char *format;

     fscanf(stream, format [0m[ , pointer ] . . .	[1m)
     FILE *stream;
     char *format;

     sscanf(s, format [0m[ , pointer ] . . .  [1m)
     char *s, *format;

DESCRIPTION
     [0m_S_c_a_n_f reads from the standard input stream [1mstdin[0m.	_F_s_c_a_n_f
     reads from the named input _s_t_r_e_a_m.  _S_s_c_a_n_f reads from the
     character string _s.  Each function reads characters, inter-
     prets them according to a format, and stores the results in
     its arguments.  Each expects as arguments a control string
     _f_o_r_m_a_t, described below, and a set of _p_o_i_n_t_e_r arguments
     indicating where the converted input should be stored.

     The control string usually contains conversion specifica-
     tions, which are used to direct interpretation of input
     sequences.  The control string may contain:

     1.  Blanks, tabs or newlines, which match optional white
	 space in the input.

     2.  An ordinary character (not %) which must match the next
	 character of the input stream.

     3.  Conversion specifications, consisting of the character
	 [1m%[0m, an optional assignment suppressing character [1m*[0m, an
	 optional numerical maximum field width, and a conversion
	 character.

     A conversion specification directs the conversion of the
     next input field; the result is placed in the variable
     pointed to by the corresponding argument, unless assignment
     suppression was indicated by [1m*[0m.  An input field is defined
     as a string of non-space characters; it extends to the next
     inappropriate character or until the field width, if speci-
     fied, is exhausted.

     The conversion character indicates the interpretation of the
     input field; the corresponding pointer argument must usually
     be of a restricted type.  The following conversion charac-
     ters are legal:



Printed 11/2/94     Revision 1.2 of 19/10/88			1






SCANF(3S)	    UNIX Programmer's Manual		SCANF(3S)



     [1m%	 [0ma single `%' is expected in the input at this point; no
	 assignment is done.

     [1md	 [0ma decimal integer is expected; the corresponding argu-
	 ment should be an integer pointer.

     [1mo	 [0man octal integer is expected; the corresponding argument
	 should be a integer pointer.

     [1mx	 [0ma hexadecimal integer is expected; the corresponding
	 argument should be an integer pointer.

     [1ms	 [0ma character string is expected; the corresponding argu-
	 ment should be a character pointer pointing to an array
	 of characters large enough to accept the string and a
	 terminating `\0', which will be added.  The input field
	 is terminated by a space character or a newline.

     [1mc	 [0ma character is expected; the corresponding argument
	 should be a character pointer.  The normal skip over
	 space characters is suppressed in this case; to read the
	 next non-space character, try `%1s'.  If a field width
	 is given, the corresponding argument should refer to a
	 character array, and the indicated number of characters
	 is read.

     [1me
	  f
	       [0ma floating point number is expected; the next field is
	 converted accordingly and stored through the correspond-
	 ing argument, which should be a pointer to a _f_l_o_a_t.  The
	 input format for floating point numbers is an optionally
	 signed string of digits possibly containing a decimal
	 point, followed by an optional exponent field consisting
	 of an E or e followed by an optionally signed integer.

     [1m[	 [0mindicates a string not to be delimited by space charac-
	 ters.	The left bracket is followed by a set of charac-
	 ters and a right bracket; the characters between the
	 brackets define a set of characters making up the
	 string.  If the first character is not circumflex (^),
	 the input field is all characters until the first char-
	 acter not in the set between the brackets; if the first
	 character after the left bracket is ^, the input field
	 is all characters until the first character which is in
	 the remaining set of characters between the brackets.
	 The corresponding argument must point to a character
	 array.

     The conversion characters [1md[0m, [1mo [0mand [1mx [0mmay be capitalized or
     preceded by [1ml [0mto indicate that a pointer to [1mlong [0mrather than
     to [1mint [0mis in the argument list.  Similarly, the conversion
     characters [1me [0mor [1mf [0mmay be capitalized or preceded by [1ml [0mto
     indicate a pointer to [1mdouble [0mrather than to [1mfloat[0m.  The



Printed 11/2/94     Revision 1.2 of 19/10/88			2






SCANF(3S)	    UNIX Programmer's Manual		SCANF(3S)



     conversion characters [1md[0m, [1mo [0mand [1mx [0mmay be preceded by [1mh [0mto
     indicate a pointer to [1mshort [0mrather than to [1mint[0m.

     The _s_c_a_n_f functions return the number of successfully
     matched and assigned input items.	This can be used to
     decide how many input items were found.  The constant [1mEOF [0mis
     returned upon end of input; note that this is different from
     0, which means that no conversion was done; if conversion
     was intended, it was frustrated by an inappropriate charac-
     ter in the input.

     For example, the call

	       int i; float x; char name[50];
	       scanf("%d%f%s", &i, &x, name);

     with the input line

	  25   54.32E-1  thompson

     will assign to _i the value 25, _x the value 5.432, and _n_a_m_e
     will contain `_t_h_o_m_p_s_o_n_\_0'.  Or,

	  int i; float x; char name[50];
	  scanf("%2d%f%*d%[1234567890]", &i, &x, name);

     with input

	  56789 0123 56a72

     will assign 56 to _i, 789.0 to _x, skip `0123', and place the
     string `56\0' in _n_a_m_e.  The next call to _g_e_t_c_h_a_r will return
     `a'.

[1mSEE ALSO
     [0matof(3), getc(3S), printf(3S)

[1mDIAGNOSTICS
     [0mThe _s_c_a_n_f functions return [1mEOF [0mon end of input, and a short
     count for missing or illegal data items.

[1mBUGS
     [0mThe success of literal matches and suppressed assignments is
     not directly determinable.











Printed 11/2/94     Revision 1.2 of 19/10/88			3



[0m