YouTip LogoYouTip

C Function Sscanf

# C Library Function - sscanf() [![Image 3: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library function **int sscanf(const char *str, const char *format, ...)** reads formatted input from a string. ## Declaration Below is the declaration for the sscanf() function. ```c int sscanf(const char *str, const char *format, ...) ## Parameters * **str** -- This is the C string, the source from which the function retrieves data. * **format** -- This is the C string that contains one or more of the following: _whitespace characters, non-whitespace characters_, and _format specifiers_. The format specifier is in the form **[=%[*]type=]**, explained in detail below: | Parameter | Description | | --- | --- | | * | This is an optional asterisk, indicating that the data is read from the stream but is 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 int (for d, i, and n), unsigned int (for o, u, and x), or float (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 that specifies the type of data to be read and how it is to be read. See the next table for details. | **sscanf Type Specifiers:** | Type | Qualifying Input | Parameter Type | | --- | --- | --- | | c | Single character: Reads the next character. If a width other than 1 is specified, the function reads width characters and passes them via the argument, storing them in consecutive positions of the array. A null character is not appended at the end. | char * | | d | Decimal integer: A leading + or - sign is optional. | int * | | e,E,f,g,G | Floating point: Includes a decimal point, an optional leading + or - sign, an optional trailing e or E, and decimal digits. Two valid examples are -732.103 and 7.12e4. | float * | | o | Octal integer. | int * | | s | String. This reads consecutive characters until a whitespace character is encountered (whitespace characters can be space, 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, in the same order as the % tags. For each format specifier in the format string that retrieves data, an additional argument should be specified. If you want to store the result of the sscanf operation in a regular variable, you should place the reference operator (&) before the identifier, for example: ```c int n; sscanf (str,"%d",&n); ## Return Value If successful, the function returns the number of items successfully matched and assigned. If an end-of-file is reached or a read error occurs, it returns EOF. ## Example The following example demonstrates the usage of the sscanf() function. ## Example ```c #include #include #include int main() { int day, year; char weekday, month, dtm; strcpy( dtm, "Saturday March 25 1989"); sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year ); printf("%s %d, %d = %sn", month, day, year, weekday ); return(0); } Let us compile and run the above program, which will produce the following result: March 25, 1989 = Saturday [![Image 4: C Standard Library - ](#) C Standard Library - ](#)
← Sorted Sets ZlexcountC Function Fscanf β†’