Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sscanf has a bug in it (VS6.0)

14 views
Skip to first unread message

Alan

unread,
Jan 1, 2010, 5:06:01 AM1/1/10
to
I've just discovered a big in sscanf (I'm using C++ in VS 6.0). I'm trying to
read an integer from a CString. If there is a leading zero, the integer is
decoded incorrectly! For example, if the CString contains "60" it returns
integer 60 but if the CString contains "060" it returns integer 48! I've
searched the help file for any mention of prohibited leadign zeros but can't
find anything so I guess it's a bug in the Microsoft library function.

I'm using this to get information from a dialog, completed by a user.

sscanf(OptDlg.m_input, "%i", &Iret);

I can't control what users put in and a leading zero is valid in my book.

Jochen Kalmbach [MVP]

unread,
Jan 1, 2010, 6:04:05 AM1/1/10
to
Hi Alan!

> I've just discovered a big in sscanf (I'm using C++ in VS 6.0). I'm trying to
> read an integer from a CString. If there is a leading zero, the integer is
> decoded incorrectly! For example, if the CString contains "60" it returns
> integer 60 but if the CString contains "060" it returns integer 48! I've
> searched the help file for any mention of prohibited leadign zeros but can't
> find anything so I guess it's a bug in the Microsoft library function.

This is not a bug, it is a feature ;)

If a leading zero is present, it will be interpreted as "octal":
060octal => 48decimal

> sscanf(OptDlg.m_input, "%i", &Iret);

You must not use "%i", instead use "%d".

This behaviour is well documented:
http://msdn.microsoft.com/en-us/library/6ttkkkhh.aspx
<quote>
An integer. Hexadecimal if the input string begins with "0x" or "0X",
octal if the string begins with "0", otherwise decimal.
</quote>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

David Lowndes

unread,
Jan 1, 2010, 6:03:26 AM1/1/10
to
>I've just discovered a big in sscanf (I'm using C++ in VS 6.0).

I'm afraid not.

> I'm trying to
>read an integer from a CString. If there is a leading zero, the integer is
>decoded incorrectly! For example, if the CString contains "60" it returns
>integer 60 but if the CString contains "060" it returns integer 48! I've
>searched the help file for any mention of prohibited leadign zeros but can't
>find anything so I guess it's a bug in the Microsoft library function.
>
>I'm using this to get information from a dialog, completed by a user.
>
>sscanf(OptDlg.m_input, "%i", &Iret);

Taken from the MSDN documentation:

"i


An integer. Hexadecimal if the input string begins with "0x" or "0X",
octal if the string begins with "0", otherwise decimal.
"

Use %d

Dave

Tim Roberts

unread,
Jan 3, 2010, 7:42:07 PM1/3/10
to
"Jochen Kalmbach [MVP]" <nospa...@kalmbach-software.de> wrote:

>Hi Alan!
>
>> I've just discovered a big in sscanf (I'm using C++ in VS 6.0). I'm trying to
>> read an integer from a CString. If there is a leading zero, the integer is
>> decoded incorrectly! For example, if the CString contains "60" it returns
>> integer 60 but if the CString contains "060" it returns integer 48! I've
>> searched the help file for any mention of prohibited leadign zeros but can't
>> find anything so I guess it's a bug in the Microsoft library function.
>
>This is not a bug, it is a feature ;)

Further, it's part of the C standard, and has been present in C since the
very beginning. It is not a Microsoft invention. You see the same thing
in code:
int x = 060;

"x" now contains 48 decimal.

Ah, kids today. They don't know any history...
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Ulrich Eckhardt

unread,
Jan 4, 2010, 3:19:51 AM1/4/10
to
Alan wrote:
> I've just discovered a big in sscanf (I'm using C++ in VS 6.0).

Nobody mentioned that yet, VC6 (aka VS98) is now 12 years old, predates the
C++ standard and is not supported by the vendor. Upgrade.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

0 new messages