Scanf Problem

Discussion in 'C Programming' started by LJ, Jul 7, 2011.

  1. LJ

    LJ Guest

    K&R says that the scanf format string may contain ordinary characters,
    which are expected to match the next non-white space character of the
    input stream.

    However when I am reading the date given as 25 /
    12 / 1988 using scanf( "%d/%d/%d", &day, &month,
    &year ); it is only reading the day. Why is it not reading the month
    and year??
     
    LJ, Jul 7, 2011
    #1
    1. Advertising

  2. LJ

    John Gordon Guest

    In <> LJ <> writes:

    > K&R says that the scanf format string may contain ordinary characters,
    > which are expected to match the next non-white space character of the
    > input stream.


    > However when I am reading the date given as 25 /
    > 12 / 1988 using scanf( "%d/%d/%d", &day, &month,
    > &year ); it is only reading the day. Why is it not reading the month
    > and year??


    Probably because your input contains spaces which the format string wasn't
    told to look for.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jul 7, 2011
    #2
    1. Advertising

  3. LJ

    LJ Guest

    On Jul 7, 9:29 am, John Gordon <> wrote:
    > In <> LJ<> writes:
    >
    > > K&R says that the scanf format string may contain ordinary characters,
    > > which are expected to match the next non-white space character of the
    > > input stream.
    > > However when I am reading the date given as 25           /
    > > 12      /         1988  using scanf( "%d/%d/%d", &day, &month,
    > > &year ); it is only reading the day. Why is it not reading the month
    > > and year??

    >
    > Probably because your input contains spaces which the format string wasn't
    > told to look for.
    >
    > --
    > John Gordon                   A is for Amy, who fell down the stairs
    >              B is for Basil, assaulted by bears
    >                                 -- EdwardGorey, "The Gashlycrumb Tinies"

    That doesn't answer my question, '/' should match the next non-
    whitespace character and in input that character is '/' only, so scanf
    shud read all the three dates
     
    LJ, Jul 7, 2011
    #3
  4. LJ

    Ike Naar Guest

    On 2011-07-07, LJ <> wrote:
    > On Jul 7, 9:29?am, John Gordon <> wrote:
    >> In <> LJ <> writes:
    >>
    >> > K&R says that the scanf format string may contain ordinary characters,
    >> > which are expected to match the next non-white space character of the
    >> > input stream.
    >> > However when I am reading the date given as 25 ? ? ? ? ? /
    >> > 12 ? ? ?/ ? ? ? ? 1988 ?using scanf( "%d/%d/%d", &day, &month,
    >> > &year ); it is only reading the day. Why is it not reading the month
    >> > and year??

    >>
    >> Probably because your input contains spaces which the format string wasn't
    >> told to look for.

    >
    > That doesn't answer my question, '/' should match the next non-
    > whitespace character and in input that character is '/' only, so scanf
    > shud read all the three dates


    It does answer your question; you should tell scanf to skip
    the whitespace.
    Read your scanf documentation. Hint: "d% / %d / %d".
     
    Ike Naar, Jul 7, 2011
    #4
  5. On Jul 7, 9:40 am, LJ <> wrote:
    >
    > That doesn't answer my question, '/' should match the next non-
    > whitespace character and in input that character is '/' only, so scanf
    > shud read all the three dates
    >

    The documentation you read is a bit confusing.

    Whitespace matches whitespace, all other characters match themselves,
    except for format fields introduced by %, and the % character itself.
    --
    Lots of programming goodies on my website
    http://www.malcolmmclean.site11.com/www
     
    Malcolm McLean, Jul 7, 2011
    #5
  6. LJ

    Guest

    LJ <> wrote:
    > K&R says that the scanf format string may contain ordinary characters,
    > which are expected to match the next non-white space character of the
    > input stream.
    >
    > However when I am reading the date given as 25 /
    > 12 / 1988 using scanf( "%d/%d/%d", &day, &month,
    > &year ); it is only reading the day. Why is it not reading the month
    > and year??


    Because the paraphrase above is not correct: ordinary characters are
    expected to match the next characters from the stream, they do not skip
    leading whitespace.
    --
    Larry Jones

    It COULD'VE happened by accident! -- Calvin
     
    , Jul 10, 2011
    #6
    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. Replies:
    21
    Views:
    1,212
    Richard Herring
    Apr 20, 2005
  2. code_wrong

    Re: printf scanf problem

    code_wrong, Aug 2, 2003, in forum: C Programming
    Replies:
    1
    Views:
    417
    /dev/null
    Aug 2, 2003
  3. Dan Pop

    Re: printf scanf problem

    Dan Pop, Aug 4, 2003, in forum: C Programming
    Replies:
    0
    Views:
    647
    Dan Pop
    Aug 4, 2003
  4. =?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,404
    those who know me have no need of my name
    Apr 3, 2006
  5. =?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:
    682
    Richard Bos
    May 2, 2006
Loading...

Share This Page