liveBook · Manning

Scanf family

As explained in 100 C++ Mistakes and How to Avoid Them

scanf is a C standard-library function that reads formatted input from a stream and converts it according to a format string.

The scanf family reads text and converts it according to format specifiers. A wrong specifier can cause undefined behavior or incorrect data, while a complex format string can be difficult to read. section 6.1

Watch for format-string surprises:

  • %d means decimal, %i can interpret a number with a leading zero as octal, and reading a double requires %lf. section 6.1
  • When legacy constraints require scanf-style functions, check specifiers carefully; typed extraction with std::istringstream offers a more readable alternative. section 6.1
Listing 6.1 Using complicated format specifiers to determine conversions
int main() {
    const char* str = "3.14159 042 boxes .3";
    double pi;
    int cats;
    int mice;
    char buffer[5];
 
    int count = sscanf(str, "%lf%*c%i%s%d", &pi, &cats, 
        buffer, &mice);                                       #1
    if (count != 4)                                           #2
        std::cout << "error reading value " << count+1 << '\n';
 
    printf("%f being eaten by %d cats in %s along with %d mice\n",
        pi, cats, mice, mice);                                #3
    return 0;
}

A complex sscanf format string illustrates how several conversions can be packed into one difficult-to-parse expression. section 6.1

Listing 6.2 Using types to determine conversions
int main() {
    std::istringstream str("3.14159 042 boxes .3");
    double pi;
    int cats;
    int mice;
    std::string buffer;
 
    str >> pi;                                              #1
    if (str.fail())
        std::cout << "error reading value 1\n";
    str >> cats;
    if (str.fail())
        std::cout << "error reading value 2\n";
    str >> buffer;
    if (str.fail())
        std::cout << "error reading value 3\n";
    str >> mice;
    if (str.fail())
        std::cout << "error reading value 4\n";
 
    std::cout << pi << " being eaten by " << cats << " cats in " << mice
        << " along with " << mice << " mice\n";             #2
    return 0;
}

Typed extraction from a string stream shows the alternative approach, with a separate failure check after each value. section 6.1

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains conversion-specifier pitfalls and readability costs, but does not provide a complete specifier reference.
Examples
Uses a string containing a floating-point value, an integer, a word, and another number to illustrate format-string complexity.
What it assumes
Assumes readers may need to look up specifier meanings and clarifies selected distinctions among %d, %i, %f, and %lf.
What you can do after
Evaluate formatted-input code for specifier mistakes and consider typed stream extraction as an alternative.
How it connects
Connects formatted input to printf, sscanf, regular expressions, and C++ string streams.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.