Spaces in scanf format string 
Author Message
 Spaces in scanf format string

I have a question about the format string while using scanf.  My K&R2 says
that the format string may contain blanks which are ignored (pg 157).
However, on two very different platforms the scanfs in the following
program behave very differently.  The only difference is the trailing space
in the format string.

#include <stdio.h>

int main(void)
{
    int i;

    printf("int: ");
    scanf("%d", &i);
    printf("you entered %d\n", i);
    printf("another int: ");
    scanf("%d ", &i);
    printf("you entered %d\n", i);

    return(0);

Quote:
}

The second scanf hangs waiting for a second input.  I know there are
problems with scanf and I generally use fgets, but this seems to contradict
K&R, and it stumped me for quite a while this morning.  Can anyone shed any
light on this?
Are my two compilers doing something wrong, or am I misreading K&R?

-=[doug]=-



Sat, 05 Jun 1999 03:00:00 GMT  
 Spaces in scanf format string

<snip>

Quote:
> I have a question about the format string while using scanf.  My K&R2 says
> that the format string may contain blanks which are ignored (pg 157).

That is incorrect.

A blank (or other whitespace) in scanf format string (except inside a
%[] specification) means `skip all white space characters'. To skip
*all* white space characters, it has to find one that is not
whitespace (and leave it unread). End-of-line, blanks, and tabs are
all whitespace.

<snip>

Quote:
>     scanf("%d ", &i);
<snip>
> The second scanf hangs waiting for a second input.  I know there are
> problems with scanf and I generally use fgets, but this seems to contradict
> K&R, and it stumped me for quite a while this morning.  Can anyone shed any
> light on this?
> Are my two compilers doing something wrong, or am I misreading K&R?

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]


Sat, 05 Jun 1999 03:00:00 GMT  
 Spaces in scanf format string



Quote:

>I have a question about the format string while using scanf.  My K&R2 says
>that the format string may contain blanks which are ignored (pg 157).

K&R2's description of scanf is incorrect (this is a well-known fault). From
the standard:

"A directive composed of white-space character(s) is executed by reading
 input up to the first nonwhite-space character (which remains unread), or
 until no more characters can be read"

So in effect white-space in the format string causes white-space in the input
data to be skipped.

Quote:
>However, on two very different platforms the scanfs in the following
>program behave very differently.  The only difference is the trailing space
>in the format string.

>#include <stdio.h>

>int main(void)
>{
>    int i;

>    printf("int: ");
>    scanf("%d", &i);
>    printf("you entered %d\n", i);
>    printf("another int: ");
>    scanf("%d ", &i);
>    printf("you entered %d\n", i);

>    return(0);

>}

>The second scanf hangs waiting for a second input.

Right - it continues to skip white-space until it encounters a non-white-
space character (or error or end-of-file). It will wait for further input
until one of these conditions is met.

Quote:
> I know there are
>problems with scanf and I generally use fgets, but this seems to contradict
>K&R, and it stumped me for quite a while this morning.  Can anyone shed any
>light on this?
>Are my two compilers doing something wrong, or am I misreading K&R?

Shock horror, it is the 3rd alternative! :-) Don't worry though, there isn't
much else wrong with K&R2 - misuse of reserved identifiers in examples is
all I can think of immediately.

--
-----------------------------------------


-----------------------------------------



Sat, 05 Jun 1999 03:00:00 GMT  
 Spaces in scanf format string

Quote:

}K&R2's description of scanf is incorrect (this is a well-known fault). From
}the standard:

Thanks for the info.  I did check a few sites for K&R errata (seebs' and
lysator) and didn't see anything about scanf.  The FAQ doesn't directly
answer the question, but 12.17 does show an example with '\n' instead of a
space.  Does anyone think this may be a good addition to the FAQ?

-=[doug]=-



Sat, 05 Jun 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Scanf: Required white space; Macros in the format

2. Examples of format strings for scanf/printf ?

3. Constants in scanf() Format String?

4. scanf() and white spaces !!??

5. Using CDaoDatabase with Excel file converts string with only spaces to empty string

6. Format specifiers and scanf

7. tiny question on format of scanf and printf

8. scanf : floating point format not linked

9. Error: scanf: floating point formats not linked

10. scanf formats not linked

11. FAQ? Syntax of scanf format code

12. printf/scanf format character(s) for 64 bit ints ??

 

 
Powered by phpBB® Forum Software