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; } |
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.