views:

120

answers:

2
char* ReadNumericFormat = "%i";
int Read(void)
{
    int Storage;
    __asm
    {
        LEA    EAX, [Storage]
        PUSH   EAX
        PUSH   DWORD PTR [ReadNumericFormat]
        CALL   DWORD PTR [scanf]
        ADD    ESP, 8
        MOV    EAX, DWORD PTR [Storage]
    }     
}

when the user enters "023919" the procedure returns 19.
this is a feature or darkness is a standard?

+7  A: 

Actually that's because you've entered an octal number.

In C, numbers starting with 0 will be interpreted as octal (base-8) literals. Hence, in your input

023919

scanf find a leading zero without an x following, so assumes it's an octal number. Then it consumes 2 and 3, until 9 which is not a valid octal digit and stop. So scanf now has

023

which is

2*8 + 3 = 19

So the procedure returns 19.


Use the format %d instead of %i to prevent this.

KennyTM
so it is a *forgotten* feature.because scanf should raise an error when the input-string is incorrect.
Behrooz
No, it's scanf acting the way it has for decades, and the way it is documented to work.
Paul Tomblin
+1  A: 

It's the same if you used scanf directly (without inline assembly). "023" is the octal representation of 19, and scanf stops at the "9" in "0239" because that can't be an octal digit.

AndiDog