delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/17/04:08:34

Date: Tue, 17 Nov 1998 11:06:31 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: "G.B.Rotman" <g DOT b DOT rotman AT stud DOT biol DOT ruu DOT nl>
cc: djgpp AT delorie DOT com
Subject: Re: scanf/gets bug?
In-Reply-To: <36506D06.965F8EC6@stud.biol.ruu.nl>
Message-ID: <Pine.SUN.3.91.981117110610.14078K-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On Mon, 16 Nov 1998, G.B.Rotman wrote:

> This simple test program doesn't even work for me,
> when I run it scanf  waits until two integers are
> given and then skips gets, I assume because of the
> newline which is still left in the buffer.

No, it works like it should, but \n in the format strings causes
`scanf' to read as many newlines and whitespace characters as
possible, so it waits for you to type any non-whitespace character and
only returns after it sees that.  The second number is actually
consumed by `gets' (try printing the `string' variable and you will
see it), and the only undesirable effect is that the prompt "give
input for gets()" is not printed where you want it.

Your original program didn't include this second prompt, so this
little subtlety didn't matter then.

Here's a slightly modified program that will do what you want:

#include <stdio.h>

int main()
{
 int  a;
 char string[25];

 puts("\ngive input for scanf()\n");
 scanf("%d", &a);
 puts("give input for gets()\n");
 scanf("\n");
 gets(string);
 puts(string);
 puts("end");
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019