sscanf_s() Function in C

sscanf_s() is secure function of sscanf() and secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable.

Syntax

int sscanf_s(const char *restrict buffer, const char *restrict format, ...);

Parameters

  • str: string used to retrieve data
  • format: string that contains the type specifier(s)
  • … (ellipsis): It indicates that the function accepts a variable number of arguments.

Note: sscanf_s has an extra parameter to specify the buffer size and actually control the limit of the input.

Return Value

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

Note: sscanf_s() will only work in Microsoft Visual Studio.

Example: C Program to Illustrate sscanf_s Function

C




// C program to illustrate sscanf_s statement
// This program will run on MS Visual studio
#include <stdio.h>
 
int main()
{
    char s[] = "3 red balls 2 blue balls";
    char str[10], str2[10];
    int i;
 
    // %*s is used to skip a word
    sscanf_s(s, "%d", &i, sizeof(i));
    sscanf_s(s, "%*d %*s %*s %*s %s", str, sizeof(str));
    sscanf_s(s, "%*d %*s %*s %*s %*s %s", str2,
             sizeof(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