Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: bill@cafe.net (Kaz Kylheku)
Subject: Re: scanf problem on Vax C vs Dec C
Date: 1997/12/19	[thread overview]
Message-ID: <67egcb$aa0$1@brie.direct.ca> (raw)
In-Reply-To: 67eef5$7ei$1@thefuture.qualcomm.com


In article <67eef5$7ei$1@thefuture.qualcomm.com>,
Ryan Moore <rmoore@qualcomm.com> wrote:
>Here's an example of a difference:
>
>#include <stdio.h>
>
>main()
>{
>        char bar[50];
>
>        printf("1: ");
>        scanf("%s", bar);
>
>        fflush(stdin);

I'm afraid that doing an output flush operation on an input stream
results in undefined behavior. Undefined behavior can easily be responsible
for one compiler behaving differently from another. Allowing fflush()
on stdin is an extension that some systems support as a non-portable
way of throwing away data that has been queued up in the input buffer.


>        printf("2: ");

You should be doing fflush(stdout) to ensure that the output appears
prior to the expectation of input from the user.

>        scanf("%c", bar);
>}
>
>On VAX C, the second scanf() will wait until the user enters a character 
>followed by the enter key.  On DEC C, the second scanf() will *not* wait for 
>the user to enter a character.

The %c conversion specifier causes the next character in the input stream
to be dequeued whether or not it is a whitespace character ot what
have you. Unlike with %s, no skipping of leading whitespace occurs. So if
there is an unremoved newline character in the input stream, it is becomes
converted.

It could be that one compiler's library supports fflush(stdin) as a way
of throwing away that extra newline.  Since the behavior is undefined,
a compiler may do anything it pleases, such as provide an extension,
arrange for the program to die, etc.

Try it with both compilers without the fflush(stdin) and post your results.



  reply	other threads:[~1997-12-19  5:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-12-17  5:00 scanf problem on Vax C vs Dec C Robert Young
1997-12-18  5:00 ` Boris Gubenko
1997-12-19  5:00   ` Ryan Moore
1997-12-19  5:00     ` Kaz Kylheku [this message]
1997-12-19  5:00     ` Chris Engebretson
1997-12-19  5:00 ` Ryan Moore
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox