spaces in scanf format string

Discussion in 'C Programming' started by stathisgotsis@hotmail.com, Nov 5, 2006.

  1. Guest

    Hello everyone,

    Trusting K&R2 i thought until recently that spaces are ignored in
    scanf's format string. Reading arguments to the contrary confused me a
    little. So i now ask:

    Is scanf("%d%d",...) different from scanf("%d %d",...) in the
    Standard's point of view?

    Thank you.
    , Nov 5, 2006
    #1
    1. Advertising

  2. said:

    > Hello everyone,
    >
    > Trusting K&R2 i thought until recently that spaces are ignored in
    > scanf's format string. Reading arguments to the contrary confused me a
    > little. So i now ask:
    >
    > Is scanf("%d%d",...) different from scanf("%d %d",...) in the
    > Standard's point of view?


    From 4.9.6.2 fscanf:

    "Input white-space characters (as specified by the isspace function)
    are skipped, unless the specification includes a [ , c , or n
    specifier."

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Nov 5, 2006
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote:
    > Hello everyone,
    >
    > Trusting K&R2 i thought until recently that spaces are ignored in
    > scanf's format string. Reading arguments to the contrary confused me a
    > little. So i now ask:
    >
    > Is scanf("%d%d",...) different from scanf("%d %d",...) in the
    > Standard's point of view?


    They differ, but so subtly that it's a "difference that
    makes no difference."

    Suppose the input is " 12\t34\n". The "%d%d" format
    matches this input as follows: The first "%d" skips the leading
    white spaces and consumes and converts the 12, then the second
    "%d" skips the tab and consumes and converts the 34. The newline
    is left unread.

    The "%d %d" format operates just a little differently, but
    has the same outcome. The first "%d" skips the leading spaces
    and converts the 12, then the space in the format matches and
    skips the tab, then the second "%d" skips nothing and converts
    the 34. As before, the newline is left unread.

    So the only difference lies in whether the tab is consumed
    by the second "%d" or by the space in the format string, and
    this difference isn't detectable "from the outside." scanf()
    has no way to tell you how many white space characters a "%d"
    did or didn't consume before converting a number.

    However, this doesn't mean that white space in the format
    string is "ignored." Most conversion specifiers automatically
    consume and ignore leading white spaces in the input until they
    find something non-white they can try to convert, but a few do
    not: "%c" and "%[" start converting immediately, whether the
    input is white or not, and "%n" doesn't read any input at all.
    So if you want white space skipped before one of these, you
    need to put some white space in the format string: " %c" or
    " %[" or " %n", for instance.

    The other situation where it becomes obvious that white
    space in the format is not ignored is when it appears at the
    end of the format string -- which is usually a mistake made
    by someone who doesn't quite understand the scanf() family yet.
    Consider our sample input of " 12\t34\n" and imagine reading
    it with scanf("%d%d\n", &x, &y). The two "%d" specifiers will
    convert their numbers as before, but what will the format's "\n"
    do? Hints: all white space in the format is equivalent in the
    sense that it matches any kind of white space in the input, and
    a format's white space can match any amount of white input. Try
    to predict what will happen, and then try it for yourself.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 5, 2006
    #3
  4. Guest

    Ο/Η Eric Sosman έγÏαψε:

    > The other situation where it becomes obvious that white
    > space in the format is not ignored is when it appears at the
    > end of the format string -- which is usually a mistake made
    > by someone who doesn't quite understand the scanf() family yet.
    > Consider our sample input of " 12\t34\n" and imagine reading
    > it with scanf("%d%d\n", &x, &y). The two "%d" specifiers will
    > convert their numbers as before, but what will the format's "\n"
    > do? Hints: all white space in the format is equivalent in the
    > sense that it matches any kind of white space in the input, and
    > a format's white space can match any amount of white input. Try
    > to predict what will happen, and then try it for yourself.


    Thank you and Richard for your answers.

    I assume the statement above will try to consume all white space
    until it encounters some non whitespace. I had never thought of this
    before, so thank you for pointing this out as well.
    , Nov 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,346
    those who know me have no need of my name
    Apr 3, 2006
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

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

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    660
    Richard Bos
    May 2, 2006
  3. John B. Matthews
    Replies:
    4
    Views:
    655
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    613
  5. NvrBst

    (f)scanf Question - Grab String of Spaces

    NvrBst, Apr 7, 2009, in forum: C Programming
    Replies:
    12
    Views:
    2,701
Loading...

Share This Page