sscanf()

Syntax
#include <stdio.h>
int sscanf(const char *s, const char *format,...);
Description

sscanf() scans string s according to the given format, storing the values in the given parameters. The format specifiers in the format tell sscanf() what to expect next. A format specifier has the format:

FormatSpec = "%" [Flag] [Width] [Size] Conversion.

where:

If the "%" sign which starts a format specification is followed by a "*", the scanned value is not assigned to the corresponding parameter.

Specifies the maximum number of characters to read when scanning the value. Scanning also stops if white space or a character not matching the expected syntax is reached.

Specifies the size of the argument to read. The meaning is given in the following table.

Table 1. Relationship of the Size parameter with allowable conversions and types
Size Allowable Conversions Parameter Type
h d, i, n short int * (instead of int *)
h o, u, x, X unsigned short int * (instead of unsigned int *)
l d, i, n long int * (instead of int *)
l o, u, x, X unsigned long int * (instead of unsigned int *)
l e, E, f, g, G double * (instead of float *)
L e, E, f, g, G long double * (instead of float *)
Conversion     = c|d|e|E|f|g|

                 G|i|n|o|p|s|
                 u|x|X|%|Range

These conversion characters tell sscanf() what to read and how to store it in a parameter. Their meaning is shown in the following table.

Table 2. Description of the action taken for each conversion.
Conversion Description
c Reads a string of exactly width characters and stores it in the parameter. If no width is given, one character is read. The argument must be a char *. The string read is not zero-terminated.
d A decimal number (syntax below) is read and stored in the parameter. The parameter must be a pointer to an integral type.
i As d, but also reads octal and hexadecimal numbers (syntax below).
e, E, f, g, or G Reads a floating point number (syntax below). The parameter must be a pointer to a floating-point type.
n The argument must be a pointer to an int. sscanf() writes the number of characters read so far to that address. If n is used together with length specifier h or l, the argument must be a pointer to a shortint or a long int.
o Reads an octal number (syntax below). The parameter must be a pointer to an integral type.
p Reads a pointer in the same format assprintf() prints it. The parameter must be a void **.
s Reads a character string up to the next white space character or at most width characters. The string is zero-terminated. The argument must be of type char *.
u As d, but the parameter must be a pointer to an unsigned integral type.
x, X As u, but reads a hexadecimal number.
% Skips a % sign in the input. Give only as %%.

You can also use a scan set to read a character string that either contains only the given characters or contains only characters not in the set. A scan set always is bracketed by left and right brackets. If the first character in the set is ^, the set is inverted (i.e., only characters not in the set are allowed). You can specify whole character ranges, e.g., A-Z specifies all upper-case letters. If you want to include a right bracket in the scan set, it must be the first element in the list, a dash (-) must be either the first or the last element. A ^ that shall be included in the list instead of indicating an inverted list must not be the first character after the left bracket.

Some examples are:

A white space in the format string skips all white space characters up to the next non-white-space character. Any other character in the format must be exactly matched by the input; otherwise sscanf() stops scanning.

The syntax for numbers as scanned by sscanf() is the following:

Listing: Syntax for Numbers


Number      = FloatNumber|IntNumber
IntNumber   = DecNumber|OctNumber|HexNumber

DecNumber   = Sign Digit {Digit} 

OctNumber   = Sign 0 {OctDigit}

HexNumber   = 0 (x|X) HexDigit{HexDigit}

FloatNumber = Sign {Digit} [.{Digit}][Exponent]

Exponent    = (e|E) DecNumber

OctDigit    = 0|1|2|3|4|5|6|7

Digit       = OctDigit |8|9

HexDigit    = Digit |A|B|C|D|E|F|

                     a|b|c|d|e|f
Return

EOF, if s is NULL; otherwise it returns the number of arguments filled in.

Note: If sscanf() finds an illegal input (i.e., not matching the required syntax), it simply stops scanning and returns successfully!