sscanf() Function in C

sscanf() is used to read formatted input from the string. Both scanf() and sscanf() functions are similar, the only difference between them is that scanf() function reads input from the user from standard input like a keyboard, and sscanf() function reads input from a string and stores the input in another string.

Syntax

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

Parameters

  • str: Input string from which we want to read data.
  • format: A string that contains the type specifier(s).
  • … (ellipsis): It indicates that the function accepts a variable number of arguments.

Return Value

  • On success, the function returns the number of values successfully read.
  • In the case of an input failure, before any data can be successfully read, EOF is returned.

Note: There should be at least as many of these arguments as the number of values stored by the format specifiers.

Example: C Program to Illustrate sscanf Function

C




// C program to illustrate sscanf statement
#include <stdio.h>
 
int main()
{
    // declaring array s
    char s[] = "3 red balls 2 blue balls";
    char str[10], str2[10];
    int i;
 
    // %*s is used to skip a word
    sscanf(s, "%d %*s %*s %*s %s %s", &i, str, str2);
 
    printf("%d %s %s \n", i, str, str2);
 
    return 0;
}


Output

3 blue balls 

Inbuilt library functions for user Input | sscanf, scanf_s, fscanf_s, sscanf_s

The C Programming Language provides various Inbuilt Library Functions for User Input. In this article, we will learn about sscanf, scanf_s, fscanf_s, sscanf_s Library Functions in C.

Similar Reads

1. sscanf() Function in C

sscanf() is used to read formatted input from the string. Both scanf() and sscanf() functions are similar, the only difference between them is that scanf() function reads input from the user from standard input like a keyboard, and sscanf() function reads input from a string and stores the input in another string....

2. scanf_s() Function in C

...

Why to use scanf_s()?

This function is specific to Microsoft compilers. It is the same as scanf, except it does not cause buffer overload. scanf_s() function is more secure than scanf() function as it provides an additional parameter to specify the buffer size that can avoid buffer overflow....

3. fscanf_s() Function in C

scanf just reads whatever input is provided from the console. C does not check whether the user input will fit in the variable that you’ve designated. If you have an array called color[3] and you use scanf for the string “Red”, it will work fine but if user enters more than 3 characters scanf starts writing into memory that doesn’t belong to colour array....

4. sscanf_s() Function in C

...

Contact Us