Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

Reply
Thread Tools

difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

 
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      04-26-2006
Hi,

Today I got a really strange problem... I've made myself a data-file and
I read in data from that file....

When I read something like this line:

03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.

with something like scanf("%i %i %i, ", &var1, &var2, &var3);

then it stopped reading data starting with "08", "09" etc... But
everything such as "07" and below worked...

Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

And the program WORKED!?!!?

Why?

I looked in my C-book and it says absolutetly the same about using %i as
%d.... I'm too tired to cut down my progrm and post a small version of
it now, and it might be that's even unnecessary because somebody know
what's happening...

Compiler: Visual studio 2005, windows xp. I suspect perhaps it's a bug,
if my C book is right that there isn't any difference between %i and %d?


Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
 
 
 
P.J. Plauger
Guest
Posts: n/a
 
      04-26-2006
"Martin Jørgensen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Today I got a really strange problem... I've made myself a data-file and I
> read in data from that file....
>
> When I read something like this line:
>
> 03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
>
> with something like scanf("%i %i %i, ", &var1, &var2, &var3);
>
> then it stopped reading data starting with "08", "09" etc... But
> everything such as "07" and below worked...
>
> Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);
>
> And the program WORKED!?!!?
>
> Why?
>
> I looked in my C-book and it says absolutetly the same about using %i as
> %d.... I'm too tired to cut down my progrm and post a small version of it
> now, and it might be that's even unnecessary because somebody know what's
> happening...
>
> Compiler: Visual studio 2005, windows xp. I suspect perhaps it's a bug, if
> my C book is right that there isn't any difference between %i and %d?


%d effectively calls strtol with a base of 10, so it assumes all numbers
it reads are decimal. %d effectively calls strtol with a base of 0, so it
adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
is an ill formed octal integer.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


 
Reply With Quote
 
 
 
 
Ben C
Guest
Posts: n/a
 
      04-26-2006
On 2006-04-26, Martin Jørgensen <(E-Mail Removed)> wrote:
> Hi,
>
> Today I got a really strange problem... I've made myself a data-file and
> I read in data from that file....
>
> When I read something like this line:
>
> 03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
>
> with something like scanf("%i %i %i, ", &var1, &var2, &var3);
>
> then it stopped reading data starting with "08", "09" etc... But
> everything such as "07" and below worked...
>
> Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);
>
> And the program WORKED!?!!?
>
> Why?


Maybe it's something to do with octal? A leading 0 is sometimes used for
octal (in C for example) 01 .. 07 are valid octal, 08 isn't.
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      04-26-2006
P.J. Plauger wrote:
> "Martin Jørgensen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > Today I got a really strange problem... I've made myself a data-file and I
> > read in data from that file....
> >
> > When I read something like this line:
> >
> > 03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
> >
> > with something like scanf("%i %i %i, ", &var1, &var2, &var3);
> >
> > then it stopped reading data starting with "08", "09" etc... But
> > everything such as "07" and below worked...
> >
> > Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);
> >
> > And the program WORKED!?!!?
> >
> > Why?
> >
> > I looked in my C-book and it says absolutetly the same about using %i as
> > %d.... I'm too tired to cut down my progrm and post a small version of it
> > now, and it might be that's even unnecessary because somebody know what's
> > happening...
> >
> > Compiler: Visual studio 2005, windows xp. I suspect perhaps it's a bug, if
> > my C book is right that there isn't any difference between %i and %d?

>
> %d effectively calls strtol with a base of 10, so it assumes all numbers
> it reads are decimal. %d effectively calls strtol with a base of 0, so it
> adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
> is an ill formed octal integer.


Your second %d should be a %i.

Robert Gamble

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2006
"P.J. Plauger" <(E-Mail Removed)> writes:
> "Martin Jørgensen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Today I got a really strange problem... I've made myself a data-file and I
>> read in data from that file....
>>
>> When I read something like this line:
>>
>> 03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
>>
>> with something like scanf("%i %i %i, ", &var1, &var2, &var3);
>>
>> then it stopped reading data starting with "08", "09" etc... But
>> everything such as "07" and below worked...
>>
>> Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);
>>
>> And the program WORKED!?!!?

[snip

> %d effectively calls strtol with a base of 10, so it assumes all numbers
> it reads are decimal. %d effectively calls strtol with a base of 0, so it
> adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
> is an ill formed octal integer.


And with %d, "010" has the value 8, not 10.

Decide whether you want numbers with leading '0' to be treated as
octal or decimal.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      04-26-2006
Keith Thompson wrote:
> "P.J. Plauger" <(E-Mail Removed)> writes:
> > "Martin Jørgensen" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> Today I got a really strange problem... I've made myself a data-file and I
> >> read in data from that file....
> >>
> >> When I read something like this line:
> >>
> >> 03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
> >>
> >> with something like scanf("%i %i %i, ", &var1, &var2, &var3);
> >>
> >> then it stopped reading data starting with "08", "09" etc... But
> >> everything such as "07" and below worked...
> >>
> >> Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);
> >>
> >> And the program WORKED!?!!?

> [snip
>
> > %d effectively calls strtol with a base of 10, so it assumes all numbers
> > it reads are decimal. %d effectively calls strtol with a base of 0, so it
> > adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
> > is an ill formed octal integer.

>
> And with %d, "010" has the value 8, not 10.


No, with %d, "010" has the value 10. With %i, "010" has the value 8.

> Decide whether you want numbers with leading '0' to be treated as
> octal or decimal.


Robert Gamble

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      04-26-2006
Martin Jxrgensen <(E-Mail Removed)> writes:

> When I read something like this line:
>
> 03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
>
> with something like scanf("%i %i %i, ", &var1, &var2, &var3);
>
> then it stopped reading data starting with "08", "09" etc... But
> everything such as "07" and below worked...
>
> Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);


%d reads a decimal integer.
%i reads a C-like integer, so that a leading 0 means "octal".
Your reference manual should have told you this.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2006
"Robert Gamble" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> "P.J. Plauger" <(E-Mail Removed)> writes:

[snip]
>> > %d effectively calls strtol with a base of 10, so it assumes all numbers
>> > it reads are decimal. %d effectively calls strtol with a base of 0, so it
>> > adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
>> > is an ill formed octal integer.

>>
>> And with %d, "010" has the value 8, not 10.

>
> No, with %d, "010" has the value 10. With %i, "010" has the value 8.
>
>> Decide whether you want numbers with leading '0' to be treated as
>> octal or decimal.


Whoops! You're right, of course. Thanks for catching my error.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      04-27-2006
Ben Pfaff wrote:
> Martin Jxrgensen <(E-Mail Removed)> writes:
>
>
>>When I read something like this line:
>>
>>03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
>>
>>with something like scanf("%i %i %i, ", &var1, &var2, &var3);
>>
>>then it stopped reading data starting with "08", "09" etc... But
>>everything such as "07" and below worked...
>>
>>Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

>
>
> %d reads a decimal integer.
> %i reads a C-like integer, so that a leading 0 means "octal".
> Your reference manual should have told you this.


Now I double-checked my book: "C primer plus, 5th edition".

It simply *doesn't* tell this... What a piece of junk...


It says (table 4.6, p.117):

%d = interpret input as a signed decimal integer
.. . .
%i = interpret input as a signed decimal integer
%o = interpret input as a signed octal integer
%x = interpret input as a signed hexadecimal integer


So the above is wrong?

And what are %o and %x for if you can do exactly the same with %0i and
probably %x-something? ? ?



Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      04-27-2006

Martin Jørgensen wrote:
> Ben Pfaff wrote:
> > Martin Jxrgensen <(E-Mail Removed)> writes:
> >
> >
> >>When I read something like this line:
> >>
> >>03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.
> >>
> >>with something like scanf("%i %i %i, ", &var1, &var2, &var3);
> >>
> >>then it stopped reading data starting with "08", "09" etc... But
> >>everything such as "07" and below worked...
> >>
> >>Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

> >
> >
> > %d reads a decimal integer.
> > %i reads a C-like integer, so that a leading 0 means "octal".
> > Your reference manual should have told you this.

>
> Now I double-checked my book: "C primer plus, 5th edition".
>
> It simply *doesn't* tell this... What a piece of junk...
>
>
> It says (table 4.6, p.117):
>
> %d = interpret input as a signed decimal integer
> . . .
> %i = interpret input as a signed decimal integer


%i will interpret the input as either a signed decimal, octal, or
hexadecimal integer based on the input format.

If the input has a leading 0, it will attempt to interpret it as an
octal value, and if the input has a leading 0x, it will attempt to
interpret it as a hexadecimal value.

Time for a new reference manual, I think. I use Harbison & Steele's
"C: A Reference Manual", currently at 5th edition.

> %o = interpret input as a signed octal integer
> %x = interpret input as a signed hexadecimal integer
>
>
> So the above is wrong?
>
> And what are %o and %x for if you can do exactly the same with %0i and
> probably %x-something? ? ?


%i is useful when you don't necessarily know ahead of time if your
input is going to be formatted as decimal, octal, or hex, and you want
to be able to handle it correctly. You'd use %d, %o, and %x when you
know the input is always supposed to be formatted in one particular way
(i.e., you're *only* reading hex-formatted input).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
Difference between << and += for Strings and Arrays. Bug? Pieter Hugo Ruby 13 11-26-2009 10:16 AM
Differences between perhaps-similar constructs? gentsquash@gmail.com Javascript 4 05-28-2008 01:23 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments