Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are sscanf and type specifiers in C?

Adnan Abbas

sscanf is a file handling function in C used to read formatted data from a string.


Below is the declaration of the function:

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


  • str is the input C string that is read to retrieve data.
  • format is the C string that specifies how the data is read, i.e., what type of data is to be read, the maximum number of characters to be read in the current reading operation, etc. It is known as type specifier.

The sscanf() function also accepts other arguments that can be used to store the result of the read operation. To store the result of sscanf in a regular variable, we need to precede it by the reference operator &.

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

Type specifiers

The following table describes the type specifiers that the compiler uses to understand the type of data in a variable while taking input. A % sign precedes each of them:

Representation Type
c single character
s string
d decimal integer
e, E, f, g, G floating point numbers
u unsigned integers
x,X Hexadecimal

Return Value

The function returns the number of filled variables that are provided as parameters. In case of any read failure, EOF is returned.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   int day, year, accountBalance;
   char accountTitle[20], month[20], dtm[100];

   strcpy( dtm, "EducativeInc March 25 1989 20000" );
   sscanf( dtm, "%s %s %d  %d %d", accountTitle, month, &day, &year,&accountBalance );

   printf(" The account balance of %s on %s %d, %d = $ %d\n",accountTitle, month, day, year, accountBalance );
Program demonstrating the use of sscanf in C

In the example above, we read a string in line 10 and store the values in the variables mentioned in the function parameters.




Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring