Why is scanf results different from user input?
-
20-09-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow