sscanf and scanf behave differently

Discussion in 'C Programming' started by effbiae, Jan 19, 2006.

  1. effbiae

    effbiae Guest

    is sscanf meant not to consume chars? run this program:

    /*ss.c*/
    int main(int argc,char *argv[])
    {char buf[3];
    int i;for(i=0;i<3;i++)
    {printf("ss: %d %s\n",sscanf(argv[1],"%2s",buf),buf);
    printf(" s: %d %s\n",scanf("%2s",buf),buf);
    }}


    with the command:

    $ gcc ss.c && echo 1234567890 |./a.out 1234567890
    ss: 1 12
    s: 1 12
    ss: 1 12
    s: 1 34
    ss: 1 12
    s: 1 56

    note that the progress of ss differs to that of s.

    kind regards,


    jack
    effbiae, Jan 19, 2006
    #1
    1. Advertising

  2. effbiae

    Chris Dollin Guest

    effbiae wrote:

    > is sscanf meant not to consume chars?


    Yes.

    --
    Chris "understanding is a three-edged sword" Dollin
    Chris Dollin, Jan 19, 2006
    #2
    1. Advertising

  3. "effbiae" <> writes:
    > is sscanf meant not to consume chars? run this program:

    [snip]

    scanf() reads from stdin. If some input has already been read, it
    won't be read again (unless you use ungetc()).

    sscanf() starts scanning from the beginning of the string you pass to
    it. It doesn't keep track internally of what it did last time. If
    you wnat sscanf() to start scanning from the middle of a string, you
    need to pass it a pointer into the middle of the string.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Jan 19, 2006
    #3
    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. Codemonkey
    Replies:
    1
    Views:
    403
    Ying-Shen Yu[MSFT]
    Oct 30, 2003
  2. Divick
    Replies:
    7
    Views:
    433
    Divick
    Sep 12, 2005
  3. Replies:
    9
    Views:
    344
    Antoninus Twink
    May 17, 2008
  4. Robert Klemme
    Replies:
    5
    Views:
    181
  5. usgog
    Replies:
    2
    Views:
    162
Loading...

Share This Page