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

Similar Threads

question about scanf 11
Problem with scanf 7
about scanf() 2
A process take input from /proc/<pid>/fd/0, but won't process it 0
sscanf question 6
Command Line Arguments 0
SSCANF 22
A very simple parser with scanf & C 49

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top