sscanf() Function
The sscanf()
function in C stdio.h reads formatted data from a string, similar to how scanf()
reads from standard input. It allows you to parse different types of data from a given string based on a specified format.
Syntax of sscanf()
int sscanf(const char *s, const char *format, ...);
Parameters
Parameter | Description |
---|---|
s | A C string from which the function reads the data. |
format | A C string that specifies the format to be used for interpreting the data (following the same rules as scanf() ). |
... | Additional arguments: pointers to already allocated storage where the extracted data will be stored. The number of these arguments must be at least equal to the number of format specifiers in the format string. |
The sscanf()
function operates much like scanf()
, except that it reads from the provided string rather than standard input. It is essential to ensure that the additional arguments point to valid memory locations of the correct type as specified in the format string.
Return Value
On success, sscanf()
returns the number of input items successfully matched and assigned. This count may be less than expected if a matching failure occurs. In case of an input failure before any data is matched, the function returns EOF
.
Examples for sscanf()
Example 1: Parsing Integers from a String
This example demonstrates how to extract integer values from a string using sscanf()
.
Program
#include <stdio.h>
int main() {
char str[] = "123 456";
int a, b;
int result = sscanf(str, "%d %d", &a, &b);
printf("Result: %d\n", result);
printf("Parsed integers: %d and %d\n", a, b);
return 0;
}
Explanation:
- A string containing two integers is defined.
sscanf()
is used with the format specifier"%d %d"
to parse two integers.- The function returns the number of successfully parsed items, which is stored in
result
. - The parsed integer values are then printed.
Program Output:
Result: 2
Parsed integers: 123 and 456
Example 2: Extracting Mixed Data Types from a String
This example illustrates how to parse different types of data such as an integer, a float, and a string from a single input string.
Program
#include <stdio.h>
int main() {
char str[] = "42 3.14 example";
int num;
float f;
char word[20];
int count = sscanf(str, "%d %f %s", &num, &f, word);
printf("Items parsed: %d\n", count);
printf("Integer: %d, Float: %f, String: %s\n", num, f, word);
return 0;
}
Explanation:
- A string containing an integer, a float, and a word is defined.
sscanf()
is used with the format specifiers"%d %f %s"
to extract the values.- The return value indicates that three items have been successfully parsed.
- The extracted values are printed accordingly.
Program Output:
Items parsed: 3
Integer: 42, Float: 3.140000, String: example
Example 3: Handling a Matching Failure
This example shows how sscanf()
behaves when the input string does not match the expected format.
Program
#include <stdio.h>
int main() {
char str[] = "Hello World";
int num;
int count = sscanf(str, "%d", &num);
if (count == 1) {
printf("Parsed number: %d\n", num);
} else {
printf("No matching items were found.\n");
}
return 0;
}
Explanation:
- A string that does not contain a valid integer is defined.
sscanf()
attempts to parse an integer using the format"%d"
.- The matching fails, so the return value is not 1.
- The program prints a message indicating that no matching items were found.
Program Output:
No matching items were found.