Scanf - Read Formatted Data from stdin

VAX11

 

Scanf - Read Formatted Data from stdin

 

scanf reads data, one character at a time from 'stdin' and stores it in the locations given by 'arguments'. 'Format-string' determines how the input fields are to be interpreted.

Each argument must be a pointer to a variable with a type that corresponds to a type specifier in 'Format-string'. 'Format-string' is a character string that contains whitespace characters, non-whitespace characters, and format specifications.

Here is a description of the arguments of scanf.

 

 

Format-string:

 

The format string is read from left to right when the first format specification is encountered, the value of the first input field is converted according to the format specification, and the converted value is then stored in the location specified by the first argument. The value of the second input field is converted according to the second format specification and stored in the second location, and so on.

Characters outside the format string- whitespace characters and non-whitespace characters, described below-should match the sequence of characters being read from the input stream.

 

Whitespace characters: blank (' '), tab ('\t'), or newline ('\n').

The scanf functions will read but not store all whitespace characters up to the next non-whitespace character in the input. One whitespace character in the format-string matches any number and combination of whitespace characters in the input.

Non-whitespace characters: Are all other ASCII characters except the percent character (%). The scanf functions will read but not store a matching non-whitespace character. If the next character scanned does not match, the function will terminate.

 

 

Format specifications: Are introduced by a percent sign (%). Format specifications cause the scanf functions to read and convert characters from the input field into specific types of values. These values are assigned to arguments in the argument list.

A format specification has the following form:

       % [*] [width] type

 

Type:

The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.

The simplest format specification contains only the percent sign and a type character  (%s, for example).

 

The various type specifications are:

 

       d    decimal integer

       D    decimal long integer

       o    octal   integer

       O    octal   long integer

       x    hex     integer

       X    Hex     long integer

       c    character

       s    string  (array of char)

 

Asterisk:

The asterisk (*) character following the percent sign suppresses assignment of the next input field. The suppressed input data is assumed to be of the type specified by the character type that follows the *. The field is scanned but not stored.

 

Width:

The width is a positive decimal integer which controls the maximum number of characters to be read from the current input field. No more than 'width' characters are converted and stored at the corresponding argument.


 

The prefix 'l' indicates the 'long' version is to be used. The corresponding argument should point to a 'long' object. The 'l' modifier can be used with the d, i, o, and x type characters.

The prefix 'h' indicates the 'short' version is to be used. The corresponding argument should point to a 'short' object.

The 'h' modifier can be used with the d, i, o and x type characters.

 

'l' and 'h' modifiers are ignored if used with any other type.

 

Returns:    R0  =

The number of fields that were successfully converted and assigned.

 A return value of EOF (-1) means an attempt was made to read at end-of-file.

 (A return value of 0 means no field was assigned).

 

 Notes:

Number of arguments is not limited. Arguments are pointers to data objects which will be stored by Scanf according to "Format string". The Arguments are pushed in reversed order. (So first data read will be saved in last argument /pointer pushed) Last Argument pushed into Stack is a pointer to the "Format-string".

 

scanf may stop reading a particular input field before it reaches a space character because:

·        the specified width was reached

·        the next character cannot be converted as specified

·        the next character conflicts with a character in the control string

 

When any of these situations occur, the next input field is considered to begin at the first unread character.