Reading strings with the sscanf() Function

I’m not a fan of the scanf() function. It’s an input function, quick enough to toss out there for a beginner to write a (somewhat) interactive programs. But the function itself is horrid, with complex arguments and dubious results.

So imagine my delight at finding its companion function, sscanf().

The sscanf() function works like scanf(), though it reads its input from a string (char array) and not standard input, hence the extra “s” in the name. Just like scanf(), the sscanf() function is touchy. Here’s the man page format:

int sscanf(const char *restrict s, const char *restrict format, ...);

The first argument is a string, s, which is scanned for the data as opposed to standard input. The rest of the arguments are the same as for scanf(): a formatting string and variables (pointers).

The sscanf() function works brilliantly — providing that the input string is clean, specifying the exact data type requested by the format string. The following code demonstrates.

2021_07_31-Lesson-a.c

#include <stdio.h>

int main()
{
    char value[] = "827";
    int cost;

    sscanf(value,"%d",&cost);
    printf("The dress cost $%d\n",cost);

    return(0);
}

The string value is processed at Line 8. It contains the characters 827, which are converted into integer value 827 by sscanf() and stored in variable cost:

The dress cost $827

As with scanf(), the sscanf() function has numerous quirks that limit its capability to magically extract data from a string. For example, say you want to ensure that the dollar sign is set in the input string. If so, you use this sscanf() function format string:

sscanf(value,"$%d",&cost);

The "$%d" means the string is scanned for a dollar sign followed immediately by a number.

char value[] = "$827";

Given the above string, the function successfully extracts the value. But if that dollar sign is missing, no value is read and variable cost is undefined. You can prevent such a thing from happening by examining the return value from sscanf(). If it’s non-zero, then a value was successfully read, as shown in the following code.

2021_07_31-Lesson-b.c

#include <stdio.h>

int main()
{
    char value[] = "827";
    int cost,r;

    r = sscanf(value,"$%d",&cost);
    if( r!=0 )
        printf("The dress cost $%d\n",cost);

    return(0);
}

This program generates no output because the $ character required by the sscanf() format string is missing in the value string.

Alas, you can’t use sscanf() to see whether a value exists within a string. As with scanf(), the sscanf() function stops reading when a whitespace character is encountered. Beyond that first whitespace character, the rest of the string doesn’t exist as far as sscanf() is concerned.

I can imagine a few instances where my code could use the sscanf() function, but only when I’m certain that the string being examined is in a specific format. Even then, I’d probably write my own extraction/parsing function just because the scanf() family of functions is so prissy.

Leave a Reply