The C library sscanf(const char *str, const char *format, ...) function reads formatted input from a string and stores the result in the provided variables.
SyntaxFollowing is the C library syntax of the sscanf() function −
int sscanf(const char *str, const char *format, ...);Parameters
This function accepts the following parameters −
The function returns the number of input items successfully matched and assigned.If the input does not match the format string, the function returns EOF or the number of successfully matched and assigned items up to that point.
Format SpecifiersBelow is the list of format specifier −
The below code demonstrates parsing an integer and a string from the input.
Below is the illustration of the C library sscanf() function.
#include <stdio.h> int main() { const char *input = "42 Alice"; int number; char name[20]; int result = sscanf(input, "%d %s", &number, name); printf("Parsed number: %d\n", number); printf("Parsed name: %s\n", name); printf("Number of items matched: %d\n", result); return 0; }Output
The above code produces following result−
Parsed number: 42 Parsed name: Alice Number of items matched: 2Example 2: Parsing a Hexadecimal and a Character
This example parses a hexadecimal number and a character from the input string.
#include <stdio.h> int main() { const char *input = "0xA5 Z"; int hexValue; char character; int result = sscanf(input, "%x %c", &hexValue, &character); printf("Parsed hex value: %x\n", hexValue); printf("Parsed character: %c\n", character); printf("Number of items matched: %d\n", result); return 0; }Output
After execution of above code, we get the following result
Parsed hex value: a5 Parsed character: Z Number of items matched: 2
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4