Easy Tutorial
❮ C Macro Errno C Exercise Example33 ❯

C Library Function - sscanf()

C Standard Library - <stdio.h>

Description

The C library function int sscanf(const char *str, const char *format, ...) reads formatted input from a string.

Declaration

Here is the declaration for the sscanf() function.

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

Parameters

Parameter Description
* This is an optional asterisk, indicating that data is read from the stream but 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 read 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 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 expected to be read. See the next table for details. |

sscanf Type Specifiers:

Type Qualified Input Parameter Type
c Single character: reads the next character. If a width different from 1 is specified, the function reads width characters, passing them in an array, stored consecutively. No null character is appended at the end. char *
d Decimal integer: the optional + or - sign in front of the number. int *
e,E,f,g,G Floating-point number: includes a decimal point, an optional sign (+ or -), an optional exponent e or E, and a decimal number. Valid examples: -732.103 and 7.12e4 float *
o Octal integer. int *
s String: this will read consecutive characters until a whitespace character is encountered (whitespace characters include spaces, newlines, and tabs). char *
u Unsigned decimal integer. unsigned int *
x,X Hexadecimal integer. int *

For each format specifier in the format string for retrieving data, an additional argument should be specified. If you want to store the result of the sscanf operation in an ordinary variable, you should place the reference operator (&) in front of the identifier, for example:

int n;
sscanf(str, "%d", &n);

Return Value

On success, the function returns the number of variables that were successfully matched and assigned. 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.

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

March 25, 1989 = Saturday

C Standard Library - <stdio.h>

❮ C Macro Errno C Exercise Example33 ❯