Widespread bug in scanf? 
Author Message
 Widespread bug in scanf?

I just noticed that the statement

    int x, y;
    scanf("%d%*[^\n]%d", &x, &y);

shows a weird behaviour. If in the input the first number is
immediately followed by a newline, scanf doesn't read y and
returns 1.  However, if two scanfs are used instead of one:

    scanf("%d%*[^\n]", &x);
    scanf("%d", &y);

everything works as expected. This happens with 3 compilers
I tried (gcc, xlc, lcc). Is it just me or is this a
widespread library bug? How do other compilers behave?

Regards,
Bernd

--
Bernd Eggink
Regionales Rechenzentrum der Uni Hamburg

http://www.*-*-*.com/
--



Wed, 19 Feb 2003 00:42:30 GMT  
 Widespread bug in scanf?

says...

Quote:
> I just noticed that the statement

>     int x, y;
>     scanf("%d%*[^\n]%d", &x, &y);

> shows a weird behaviour. If in the input the first number is
> immediately followed by a newline, scanf doesn't read y and
> returns 1.  However, if two scanfs are used instead of one:

>     scanf("%d%*[^\n]", &x);
>     scanf("%d", &y);

> everything works as expected. This happens with 3 compilers
> I tried (gcc, xlc, lcc). Is it just me or is this a
> widespread library bug? How do other compilers behave?

This is exactly how things are supposed to work: from the viewpoint
of scanf, a conversion that doesn't match any input is considered to
have failed.  When a conversion fails, scanf returns without making
any attempt at reading more input.

When you break the reading up into two calls to scanf, this behavior
is covered up: the first call may be returning because it's matched
input for all the conversions, OR it may be returning because the
last conversion you specified failed.  Since you're not actually
producing anything from the last conversion, the difference is more
or less irrelevant.

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.
--



Thu, 20 Feb 2003 02:05:51 GMT  
 Widespread bug in scanf?

Quote:

>     scanf("%d%*[^\n]%d", &x, &y);

> shows a weird behaviour. If in the input the first number is
> immediately followed by a newline, scanf doesn't read y and
> returns 1.

"%[...]" expects a nonempty sequence of characters.  Because the
character after the first number is a newline, "%*[^\n]" doesn't
match and scanf() returns.

Quote:
>     scanf("%d%*[^\n]", &x);
>     scanf("%d", &y);

"%*[^\n]" doesn't match here either, but that doesn't prevent the
second call from reading the newline and the number after it.

If your data looks like this:

        402 Payment Required
        305 Use Proxy
        180
        200 OK

then I guess your second method is the easiest way to parse it.
--



Thu, 20 Feb 2003 03:46:06 GMT  
 Widespread bug in scanf?
Isn't the answer to avoid scanf and use something else?


Quote:
> I just noticed that the statement

>     int x, y;
>     scanf("%d%*[^\n]%d", &x, &y);

> shows a weird behaviour. If in the input the first number is
> immediately followed by a newline, scanf doesn't read y and
> returns 1.  However, if two scanfs are used instead of one:

>     scanf("%d%*[^\n]", &x);
>     scanf("%d", &y);

> everything works as expected. This happens with 3 compilers
> I tried (gcc, xlc, lcc). Is it just me or is this a
> widespread library bug? How do other compilers behave?

> Regards,
> Bernd

> --
> Bernd Eggink
> Regionales Rechenzentrum der Uni Hamburg

> http://www.rrz.uni-hamburg.de/eggink/BEggink.html
> --


--



Wed, 26 Feb 2003 13:05:46 GMT  
 Widespread bug in scanf?
My understanding of scanf has always been that it reads text, then
applies the text to the the format string. What you're expecting is to
have the format string interpreted as characters are being read in.
( ie: scanf is similair to fgets( stdin, ... ), sscanf( ) ).

Quote:

> Isn't the answer to avoid scanf and use something else?



> > I just noticed that the statement

> >     int x, y;
> >     scanf("%d%*[^\n]%d", &x, &y);

> > shows a weird behaviour. If in the input the first number is
> > immediately followed by a newline, scanf doesn't read y and
> > returns 1.  However, if two scanfs are used instead of one:

> >     scanf("%d%*[^\n]", &x);
> >     scanf("%d", &y);

> > everything works as expected. This happens with 3 compilers
> > I tried (gcc, xlc, lcc). Is it just me or is this a
> > widespread library bug? How do other compilers behave?

> > Regards,
> > Bernd

> > --
> > Bernd Eggink
> > Regionales Rechenzentrum der Uni Hamburg

> > http://www.rrz.uni-hamburg.de/eggink/BEggink.html
> > --

> --


--



Fri, 28 Feb 2003 22:08:14 GMT  
 Widespread bug in scanf?


Quote:
>I just noticed that the statement

>    int x, y;
>    scanf("%d%*[^\n]%d", &x, &y);

>shows a weird behaviour. If in the input the first number is
>immediately followed by a newline, scanf doesn't read y and
>returns 1.  However, if two scanfs are used instead of one:

>    scanf("%d%*[^\n]", &x);
>    scanf("%d", &y);

>everything works as expected. This happens with 3 compilers
>I tried (gcc, xlc, lcc). Is it just me or is this a
>widespread library bug? How do other compilers behave?

>Regards,
>Bernd

think about what you are asking scanf to parse in the first
example -- you are asking for two numbers without an intervening
newline -- on the same line -- scanf returns an error after the
first number is parsed if there is a newline before the second
number -- that makes sense to me!

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

                                use address above to reply
--



Fri, 28 Feb 2003 22:09:17 GMT  
 Widespread bug in scanf?

Quote:

> Isn't the answer to avoid scanf and use something else?

No. It's perfectly enough to just use scanf _correctly_. Really
understanding how it's supposed to work is what makes the difference.
Just because it doesn't do what you thought it would doesn't mean
there's a bug in scanf() --- it might just as easily be a bug in your
understanding of scanf().

There are other reasons to avoid scanf() (--> use fgets() + sscanf()
instead), but the case at hand is not about that point.

Quote:


>>     scanf("%d%*[^\n]%d", &x, &y);

--

Even if all the snow were burnt, ashes would remain.
--



Sun, 02 Mar 2003 03:41:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. how widespread is this cpp bug?

2. how widespread is this cpp bug?

3. C Compiler Bug (possibly widespread)

4. scanf: is this a bug?

5. scanf bug when retrieving a float value from a pointer to a float, in a structure

6. Possible scanf bug?

7. scanf feature or bug?...

8. MS C/C++ 7.00 ? Bug (scanf, gets)

9. Usage of uint not widespread

10. C99 - is it in widespread use?

11. WIDESPREAD INCOMPETENCE AT BELL ATLANTIC

12. Widespread C++ Competency Gap?

 

 
Powered by phpBB® Forum Software