C library functions -sscanf()
C Standard Library - <stdio.h>
Description
C Library Functionsint sscanf(const char *str, const char *format, ...)Read formatted input from a string.
Declaration
Below is the declaration of the sscanf() function.
int sscanf(const char *str, const char *format, ...)
Parameter
- str-- This is a C string that the function retrieves data from.
- format-- This is a C string that contains one or more of the following items:whitespace characters, non-whitespace charactersandformat specifiers。
The format specifier has the form[=%[*][width][modifiers]type=], with detailed explanation as follows:
| Parameter | Description |
|---|---|
| * | This is an optional asterisk, indicating that the data is read from the stream but can be ignored, i.e., it is not stored in the corresponding argument. |
| width | This specifies the maximum number of characters to be read in the current reading operation. |
| modifiers | Specifies a size different from integer (for d, i, and n), unsigned integer (for o, u, and x), or floating-point (for e, f, and g) for the data pointed to by the corresponding additional argument: h : short int (for d, i, and n), or unsigned short int (for o, u, and x) l : long int (for d, i, and n), or unsigned long int (for o, u, and x), or double (for e, f, and g) L : long double (for e, f, and g) |
| type | A character specifying the type of data to be read and how it is read. See the next table for details. |
sscanf type specifiers:
| Type | Qualifying input | argument type |
|---|---|---|
| c | Single character: reads the next character. If a width other than 1 is specified, the function reads width characters and stores them in successive positions of the array passed as an argument. No null character is appended at the end. | char * |
| d | Decimal integer: a + or - sign before the number is optional. | int * |
| e,E,f,g,G | Floating-point number: contains a decimal point, an optional leading sign + or -, an optional trailing character e or E, and decimal digits. Two valid examples are -732.103 and 7.12e4 | float * |
| o | Octal integer. | int * |
| s | String. This reads successive characters until a whitespace character is encountered (whitespace characters can be blank, newline, and tab). | char * |
| u | Unsigned decimal integer. | unsigned int * |
| x,X | Hexadecimal integer. | int * |
Additional arguments-- This function accepts a series of pointers as additional arguments, each pointing to an object whose type is specified by the corresponding % tag in the format string. The arguments are in the same order as the % tags.
For each format specifier in the format string that retrieves data, an additional parameter should be specified. If you want to store the result of the sscanf operation in an ordinary variable, you should place the reference operator (&) before the identifier, for example:
int n; sscanf (str,"%d",&n);
Return Value
If successful, the function returns the number of successful matches and assignments. If the end of the file is reached or a read error occurs, it returns EOF.
Example
The following example demonstrates the use of the sscanf() function.
Example
#include <stdlib.h>
#include <string.h>
int main()
{
int day, year;
char weekday[20], month[20], dtm[100];
strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );
printf("%s %d, %d = %s\n", month, day, year, weekday );
return(0);
}
Let us compile and run the above program, which will produce the following result:
March 25, 1989 = Saturdayother extensions