'Why does sscanf read more than expected?

sscanf supports %n to count how many bytes are read.

Why does sscanf sometimes read additional bytes?

#include <stdio.h>

int main()
{
    char *data = "X\n \n\x09\n \x10\n";
    int len = 0;

    sscanf(data, "X%n", &len);
    printf("%i\n", len);

    sscanf(data, "X\n%n", &len);
    printf("%i\n", len);

    return 0;
}

This program prints:

1
7

I would expect:

1
2

(1 for X and 2 for X\n.)

Why does it read more bytes than expected?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source