Scans formatted text from a string.
Include
<stdio.h>
Prototype
int sscanf(const char *s, const char *format,
...);
Arguments
s
format
Return Value
Returns the number of items successfully converted and assigned. If no items are
assigned, a 0 is returned. EOF
is returned if an input error is
encountered before the first conversion.
Remarks
The format argument has the same syntax and use that it has in
scanf
.
Example
#include <stdio.h>
int main(void)
{
char s[] = "5 T green 3000000.00";
int number, items;
char letter;
char color[10];
float salary;
items = sscanf(s, "%d %c %s %f", &number, &letter,
&color, &salary);
printf("Number of items scanned = %d\n", items);
printf("Favorite number = %d\n", number);
printf("Favorite letter = %c\n", letter);
printf("Favorite color = %s\n", color);
printf("Desired salary = $%.2f\n", salary);
}
Example Output
Number of items scanned = 4
Favorite number = 5
Favorite letter = T
Favorite color = green
Desired salary = $3000000.00