Python sscanf – sscanf C Library Function

Python sscanf: The function int sscanf(const char *str, const char *format, …); reads formatted data from the given string pointed by str and stores them in the variables pointed by the additional arguments. Additional arguments must point to variables of the same type as specified in the format.

The sscanf function is similar to scanf function but instead of reading data stdin, it reads data from given string(character array).

Format is a null terminated string that contains Whitespace character, Non-whitespace character and Format specifiers.

The syntax of the format specifier is as follows: [*][width][length]type

Below is the description of each component of format specifier

Component Description
* This is an optional starting asterisk indicates that the data to be read from the stream but will be ignored. It won’t get stored in any memory location.
width This specifies the maximum number of characters to be read from stream for this read operation.
length Specifies a size different from the data pointed by the corresponding additional argument.
type A character specifying the type of data to be read from stream and in what format it is expected to be read.

sscanf type specifiers

Specifier Description
d Decimal integer. Any number of decimal digits (0-9), optionally preceded by a ‘+’ or ‘-‘ sign.
u Unsigned decimal integer.
o Octal integer. Any number of octal digits (0-7), optionally preceded by a ‘+’ or ‘-‘ sign.
x Hexadecimal integer. Any number of hexadecimal digits (0-9, a-f, A-F), and optionally preceded by a ‘+’ or ‘-‘ sign.
f, e or g Floating point number. A decimal number, may containing a decimal point, optionally preceded by a ‘+’ or ‘-‘ sign and optionally followed by the e or E character and a decimal integer.
c Character Reads a character from stream. If a width greater than one is specified, the function reads width characters and stores them in the successive locations of the array passed as argument.
s Sequence of Characters It will keep on reading characters from stream untill it finds a white space character(whitespace characters are considered to be blank, newline and tab). A terminating null character (‘\0’) is added at the end of the stored characters.
[characters] It will read only those characters specified between the brackets.
[^characters] It will read only those characters which is not specified between the brackets.

Function prototype of sscanf

int sscanf(const char *str, const char *format, ...);
  • str : This is the pointer to an character array from where the formatted string to be read.
  • format : This is a null terminated string that contains Whitespace character, Non-whitespace character and Format specifiers.
  • additional arguments : As per the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the data read from stream to be stored.

Return value of sscanf

C sscanf example: On success, this function returns the total number of objects successfully read from string, it may or may not be same as the expected number of items specified in format string.

C program using sscanf function

How to use sscanf in c: The following program shows the use of sscanf function to read formatted data from a string.

sscanf C Library Function

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   int age;
   float height;
   char name[40], place[40];
   char *string = "Jack 25 London 5.7";
    
   sscanf(string, "%s %d %s %f", name, &age, place, &height);
 
   printf("Name : %s\n", name);
   printf("Age : %d\n", age);
   printf("Place : %s\n", place);
   printf("Height : %f\n", height);
    
   return(0);
}

Output

Name : Jack
Age : 25
Place : London
Height : 5.700000