C programming stdio.h function - int sscanf(const char *str, const char *format, ...)

https:‮igi.www//‬ftidea.com

sscanf function is a part of the stdio.h library in C programming language. It is used to read formatted input from a string instead of a file.

The syntax of sscanf function is:

int sscanf(const char *str, const char *format, ...);

where:

  • str is the string containing the input data to be read
  • format is a string that specifies the format of the input data
  • ... is a variable argument list that will receive the input data

The function returns the number of input items successfully matched and assigned. If the function fails to match any input item, it returns EOF.

Here is an example of using sscanf function:

#include <stdio.h>

int main() {
    char str[] = "John 25";
    char name[20];
    int age;

    sscanf(str, "%s %d", name, &age);

    printf("Name: %s\nAge: %d\n", name, age);

    return 0;
}

Output:

Name: John
Age: 25