Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – sscanf()

C Library Function - sscanf()

C Standard Library - <stdio.h> C Standard Library -

Description

C Library Function **int sscanf(const char str, const char format, ...) Reads formatted input from a string.

Declaration

Below is the declaration of the sscanf() function.



int sscanf(const char *str, const char *format, ...)

Parameters

Parameters Description
* This is an optional asterisk indicating that data is read from the stream stream but is ignored, that is, it is not stored in the corresponding argument.
width This specifies the maximum number of characters to be read in the current operation.
modifiers : 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 read. See the next table for details.

sscanf type specifiers:

Type Valid input Argument type
c Single character: reads the next character. If a width other than 1 is specified, the function reads width characters and, passing them via the argument, stores them in consecutive positions of the array. No null character is appended at the end. char *
d Decimal integer: the + or - sign before the number is optional. int *
e,E,f,g,G Floating-point number: includes a decimal point, an optional leading + or - sign, an optional trailing e or E character, and a decimal number. 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 (a whitespace character can be a space, newline, or 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, with the arguments 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 normal 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 successfully matched and assigned items. If the end of the file is reached or a read error occurs, EOF is returned.

Example

The following example demonstrates the use of the sscanf() function.

Example

#include

#include

#include

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's compile and run the above program, which will produce the following result:



March 25, 1989 = Saturday

C Standard Library - <stdio.h> C Standard Library -