This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: scanf("%[^\n]",string) question
- To: puneet762k1 at yahoo dot co dot in
- Subject: Re: scanf("%[^\n]",string) question
- From: DJ Delorie <dj at redhat dot com>
- Date: Fri, 2 Nov 2001 16:43:17 -0500
- CC: gcc-bugs at gcc dot gnu dot org
- References: <20011102051927.61128.qmail@web8103.in.yahoo.com>
> I am using Linux redhat 6.2 nd using gcc
> ((egcs-1.1.2) compiler to compile my program. I am
> facing problem with scanf(). The sample Program looks
> like this
This is not a gcc bug, but a problem with your code, however...
> printf("\nEnter Your FName & Last Name:");
You should add "fflush(stdout);" here if you expect this to be
portably printed before the scanf below.
> --->scanf("%[^\n]",a) ;
You tell it to read everything but the \n. The \n is *still* in the
buffer, so the next scanf sees the \n first, and stops. Try adding \n
after the ].
Or better, use fgets() to read in a *line* into a buffer, and sscanf
to parse the line. Much more reliable that way.