sscanf and scanf behave differently

E

effbiae

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
 
K

Keith Thompson

effbiae said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top