Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sscanf and scanf behave differently

Thread Tools

sscanf and scanf behave differently

Posts: n/a
is sscanf meant not to consume chars? run this program:

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,


Reply With Quote
Chris Dollin
Posts: n/a
effbiae wrote:

> is sscanf meant not to consume chars?


Chris "understanding is a three-edged sword" Dollin
Reply With Quote
Keith Thompson
Posts: n/a
"effbiae" <(E-Mail Removed)> writes:
> is sscanf meant not to consume chars? run this program:


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) Removed) <>
San Diego Supercomputer Center <*> <>
We must do something. This is something. Therefore, we must do this.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
multithreading ; linux adn windows behave differently C Programming 9 05-17-2008 09:22 AM
same html code behave differently in IE and FireFox usgog Javascript 2 03-24-2007 12:13 AM
C++ Exception: Why does this program behave differently for pointer and array of pointer Divick C++ 7 09-12-2005 05:49 AM
Why do #dup and #clone behave differently with respect to instance methods Robert Klemme Ruby 5 05-18-2004 09:21 AM
Do threads behave Differently on Windows 98? Codemonkey ASP .Net 1 10-30-2003 04:16 AM