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.
Contact Us