Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a newbie question.

Thread Tools

a newbie question.

John Bode
Posts: n/a
On Mar 12, 1:27 am, Mike <(E-Mail Removed)> wrote:
> Hi
> #include<stdio.h>
> main()
> {
> char s[]="Taiwan University.";
> float a;
> scanf("%f",&a);
> printf("%f",a);
> printf("input string:");
> gets(s);
> puts("%s",s);
> }
> After compiled by Visual Studio, the above program can input a.
> But after it shows a, then I cannot enter s string.
> Why?
> Mike

When you type in your floating point number (3.14159, for example) and
hit Enter, the input stream looks like this:


After the call to scanf(), the newline character is still left in the
input stream:


because scanf() with the %f conversion specifier stops reading at the
first character that isn't part of a valid floating-point constant.

gets() reads up to the next newline character; since there's already a
newline character in the input stream left over from the last input
operation, it returns immediately.

There are two ways around this. The easy way is to stick a getchar()
after the scanf() to consume the newline. A somewhat more involved
(but ultimately safer) way is to read your floating-point number in as
a string using fgets() (*NOT* gets()), and then convert using sscanf()
or strtod().

Do not use gets(); it *WILL* introduce a point of failure in your
code. Use fgets() instead:

fgets(s, sizeof s, stdin);

Unlike gets(), fgets() writes the trailing newline to your buffer.
Reply With Quote
Keith Thompson
Posts: n/a
Ralf Damaschke <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> Mike <(E-Mail Removed)> writes:
>>> char s[]="Taiwan University.";

>> it's a fairly concise way to say that
>> you want s to be big enough to hold that particular string value.
>> An alternate way to do the same thing is:
>> char s[sizeof "Taiwan University." + 1];
>> but it's easy to forget the "+ 1" (needed to allow for the
>> terminating '\0').

> Oops, it seems as easy to confuse sizeof with strlen, too.

Absolutely; good catch. (sizeof "string literal" already includes the
trailing '\0'; the +1 is unnecessary.)

(As it happens, I realized I had made this error as I was driving to
work this morning; I haven't had a chance to post until now.)

Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply With Quote
David Thompson
Posts: n/a
On Wed, 12 Mar 2008 06:59:36 GMT, Micah Cowan <(E-Mail Removed)>

> Mike <(E-Mail Removed)> writes:

<snip other points>
> > printf("input string:");

> If stdout is line-bufered, you won't see this prompt before it starts
> expecting input.

Actually, you probably will; see 7.19.3p3 about "input is requested".
(Barring the UB earlier for an uninit float.) But you _might_ not.

> > gets(s);
> > puts("%s",s);

> Since gets() strips newlines, puts() won't print one, so you may not
> see this output either.

_printf_, which the OP apparently meant, wouldn't add a newline,
but _puts()_ would -- but would be called as puts(s) .

> Aside from that, gets() is unsafe.

That too.

- formerly david.thompson1 || achar(64) ||
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
newbie with newbie questions JohnE ASP .Net 3 08-17-2009 10:10 PM
VONAGE Newbie w/newbie question VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM