vsscanf() Function

The vsscanf() function in C stdio.h reads formatted data from a string into a variable argument list. It interprets the contents of an input string based on a provided format and stores the extracted values into variables via a variable argument list. This function is particularly useful when creating wrapper functions that accept a variable number of arguments for formatted input.


Syntax of vsscanf()

</>
Copy
int vsscanf(const char *s, const char *format, va_list arg);

Parameters

ParameterDescription
sA C string that serves as the source for data extraction.
formatA C string specifying the format to interpret the input, following the same rules as used by scanf().
argA variable argument list, previously initialized with va_start, where the parsed data will be stored.

Internally, vsscanf() retrieves arguments from the variable argument list using va_arg, which alters the state of the list. It is crucial to initialize the argument list with va_start before calling this function and to clean it up with va_end afterwards.


Return Value

On success, vsscanf() returns the number of input items successfully assigned. This count may be less than the expected number if a matching failure occurs. If an input failure occurs before any conversion is made, the function returns EOF.


Examples for vsscanf()

Example 1: Basic Usage with a Custom Parsing Function

This example demonstrates how to implement a custom parsing function that uses vsscanf() to extract an integer, a floating-point number, and a string from an input string.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>

int parse_data(const char *str, const char *format, ...) {
    va_list args;
    int ret;
    va_start(args, format);
    ret = vsscanf(str, format, args);
    va_end(args);
    return ret;
}

int main() {
    int num;
    float f;
    char word[20];
    const char *input = "42 3.14 hello";
    
    int count = parse_data(input, "%d %f %s", &num, &f, word);
    printf("Parsed %d items.\n", count);
    printf("Number: %d, Float: %f, Word: %s\n", num, f, word);
    
    return 0;
}

Explanation:

  1. A helper function parse_data is defined, which initializes a variable argument list and uses vsscanf() to parse the input string based on the provided format.
  2. In the main function, variables for an integer, a float, and a string are declared.
  3. The input string "42 3.14 hello" is parsed using the format "%d %f %s", storing the values into the respective variables.
  4. The program then prints the number of successfully parsed items along with the extracted values.

Program Output:

Parsed 3 items.
Number: 42, Float: 3.140000, Word: hello

Example 2: Handling Partial Matches with Incorrect Format

This example illustrates the behavior of vsscanf() when the input string does not fully match the expected format.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>

int parse_data(const char *str, const char *format, ...) {
    va_list args;
    int ret;
    va_start(args, format);
    ret = vsscanf(str, format, args);
    va_end(args);
    return ret;
}

int main() {
    int a, b;
    const char *input = "123 abc";
    
    int count = parse_data(input, "%d %d", &a, &b);
    printf("Parsed %d items.\n", count);
    if(count == 2) {
        printf("a: %d, b: %d\n", a, b);
    } else {
        printf("Input did not match the expected format.\n");
    }
    
    return 0;
}

Explanation:

  1. The same parse_data function is used to attempt parsing two integers from the input string.
  2. The input string "123 abc" contains a valid integer followed by a non-numeric token.
  3. vsscanf() successfully parses the first integer but fails on the second token, returning a count of 1.
  4. The program checks the return value and prints an appropriate message if the expected number of items is not parsed.

Program Output:

Parsed 1 items.
Input did not match the expected format.

Example 3: Parsing a Formatted String with Fixed Labels

This example demonstrates how to parse a string that includes fixed text labels along with the data values using vsscanf().

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>

int parse_data(const char *str, const char *format, ...) {
    va_list args;
    int ret;
    va_start(args, format);
    ret = vsscanf(str, format, args);
    va_end(args);
    return ret;
}

int main() {
    char name[20];
    int age;
    const char *input = "Name: John Age: 25";
    
    int count = parse_data(input, "Name: %s Age: %d", name, &age);
    printf("Parsed %d items.\n", count);
    printf("Name: %s, Age: %d\n", name, age);
    
    return 0;
}

Explanation:

  1. The parse_data function is used to extract data from a string that includes fixed labels.
  2. The input string "Name: John Age: 25" contains the labels "Name:" and "Age:" along with the actual data.
  3. The format string "Name: %s Age: %d" is used to parse and extract the name and age values.
  4. The program prints the number of successfully parsed items, followed by the extracted name and age.

Program Output:

Parsed 2 items.
Name: John, Age: 25