#include <stdio.h>
int sscanf(const char *s, const char *format, ...);
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:
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 following table describes the meaning of the size argument.
| 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 *) |
These conversion characters tell sscanf() what to read and how to store it in a parameter. Their meaning is shown in the following table.
| Conversion | Description |
|---|---|
| c | Reads a string of exactly width characters and stores it in the parameter. If no width is given, reads one character. The argument must be a char *. The string read is not zero-terminated. |
| d | Reads as a decimal number (syntax below) and stores in the parameter. The parameter must be a pointer to an integral type. |
| i | Reads as d (above), 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 longint. |
| 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 as sprintf() 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 | Reads as d (above), but the parameter must be a pointer to an unsigned integral type. |
| x, X | Reads as u (above), but reads a hexadecimal number. |
| % | Skips a % sign in the input. Give only as %%. |
Range = [[^]List]
List = Element {Element}
Element = <any char> [-<any char>]
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 (that is, only characters not in the set are allowed). You can specify whole character ranges, (for example, A-Z specifies all upper-case letters). 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. To include a ^ in the list (instead of indicating an inverted list) ensure that the ^ is not the first character after the left bracket.
Some examples are:
[A-Za-z] Allows all upper- and lower-case characters.
[^A-Z] Allows any character that is not an upper-case character.
[]abc] Allows ], a, b and c.
[^]abc] Allows any character except ], a, b and c.
[-abc] Allows -, a, b and c.
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:
Number = FloatNumber|IntNumber
IntNumber = DecNumber|OctNumber|HexNumber
DecNumber = SignDigit{Digit}
OctNumber = Sign0{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
EOF, if s is NULL; otherwise it returns the number of arguments filled in.