Search code examples
cintegerconstantsscanfconversion-specifier

sscanf does not work properly for eight and nine with leading zero


I've been trying to scan integers with sscanf() from strings with leading zeros (e.g. '03').

However, it works fine but only until '07'. Starting with '08', the strings will be read as 0.

Below you'll find my code and the output. Thank you for your help!

#include <stdio.h>
#include <stdlib.h>

int main() {

    char string_six[3] = "06";
    char string_seven[3] = "07";
    char string_eight[3] = "08";
    char string_nine[3] = "09";

    int six = -1;
    int seven = -1;
    int eight = -1;
    int nine = -1;

    sscanf(string_six, "%i", &six);
    sscanf(string_seven, "%i", &seven);
    sscanf(string_eight, "%i", &eight);
    sscanf(string_nine, "%i", &nine);

    printf("Six: %i\n",six);
    printf("Seven: %i\n",seven);
    printf("Eight: %i\n",eight);
    printf("Nine: %i\n",nine);

    return 0;    
}

Output:

Six: 6
Seven: 7
Eight: 0
Nine: 0

Solution

  • You need to use the conversion specifier %d instead of %i.

    From the C Standard (7.21.6.2 The fscanf function)

    d Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtol function with the value 10 for the base argument. The corresponding argument shall be a pointer to signed integer.

    i Matches an optionally signed integer, whose format is the same as expected for the subject sequence of the strtol function with the value 0 for the base argument. The corresponding argument shall be a pointer to signed integer.

    And (7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions)

    3 If the value of base is zero, the expected form of the subject sequence is that of an integer constant as described in 6.4.4.1, optionally preceded by a plus or minus sign, but not including an integer suffix.

    And at last (6.4.4.1 Integer constants)

    integer-constant:
        decimal-constant integer-suffixopt
        octal-constant integer-suffixopt
        hexadecimal-constant integer-suffixopt