C library function - sscanf()

The C library function int sscanf(const char *str, const char *format, ...) reads formatted input from a string.
Following is the declaration for sscanf() function.
int sscanf(const char *str, const char *format, ...)
str − This is the C string that the function processes as its source to retrieve the data.
str − This is the C string that the function processes as its source to retrieve the data.
format − This is the C string that contains one or more of the following items: Whitespace character, Non-whitespace character and Format specifiers
A format specifier follows this prototype: [=%[*][width][modifiers]type=]
format − This is the C string that contains one or more of the following items: Whitespace character, Non-whitespace character and Format specifiers
A format specifier follows this prototype: [=%[*][width][modifiers]type=]
*
This is an optional starting asterisk, which indicates that the data is to be 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 reading operation.
modifiers
Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of 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 next table.
other arguments − This function expects a sequence of pointers as additional arguments, each one pointing to an object of the type specified by their corresponding %-tag within the format string, in the same order.
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 a sscanf operation on a regular variable you should precede its identifier with the reference operator, i.e. an ampersand sign (&), like: int n; sscanf (str,"%d",&n);
other arguments − This function expects a sequence of pointers as additional arguments, each one pointing to an object of the type specified by their corresponding %-tag within the format string, in the same order.
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 a sscanf operation on a regular variable you should precede its identifier with the reference operator, i.e. an ampersand sign (&), like: int n; sscanf (str,"%d",&n);
On success, the function returns the number of variables filled. In the case of an input failure before any data could be successfully read, EOF is returned.
The following example shows the usage of 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 us compile and run the above program that will produce the following result −
March 25, 1989 = Saturday


    12 Lectures 
     2 hours 

 Nishant Malik

    12 Lectures 
     2.5 hours 

 Nishant Malik

    48 Lectures 
     6.5 hours 

 Asif Hussain

    12 Lectures 
     2 hours 

 Richa Maheshwari

    20 Lectures 
     3.5 hours 

 Vandana Annavaram

    44 Lectures 
     1 hours 

 Amit Diwan
 Print
 Add Notes
 Bookmark this page