This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: scanf("%[^\n]",string) question



>    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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]