ghot@s.ms.uky.edu (Allan Adler) (02/05/91)
Perhaps someone can clear up a mystery. The enclosed program compiles
correctly using IBMC on an IBM3090. When I run it, it refuses to execute
the first printf until after the user has typed something in for the
scanf statement, whereupon it executes both printf statements.
In order to get the program to work correctly, the line
printf("Please type your name: ");
has to be replaced by
printf("Please type your name: \n");
I would never have predicted this based on my previous experience with C
and my readings. Can someone tell me if this behavior is consistent with
the ANSI standard ?
Allan Adler
ghot@ms.uky.edu
==========================================================================
#include <stdio.h>
main()
{
char name[20];
printf("Please type your name: ");
scanf("%s",name);
printf("You can do it, %s!\n",name);
}
Allan
steve@taumet.com (Stephen Clamage) (02/06/91)
ghot@s.ms.uky.edu (Allan Adler) writes: >When I run [this program], it refuses to execute >the first printf until after the user has typed something in for the >scanf statement, whereupon it executes both printf statements. [program works as desired when '\n' added to printf statements] This is an issue of system buffering, and when output characters appear on the user's terminal. The whole area is implementation-defined (ANSI C standard, section 4.9.3). A conforming implementation may show the behavior you describe, so long as it documents it. -- Steve Clamage, TauMetric Corp, steve@taumet.com
gwyn@smoke.brl.mil (Doug Gwyn) (02/06/91)
In article <1991Feb4.234251.14836@ms.uky.edu> ghot@s.ms.uky.edu (Allan Adler) writes: >In order to get the program to work correctly, the line >printf("Please type your name: "); >has to be replaced by >printf("Please type your name: \n"); >I would never have predicted this based on my previous experience with C >and my readings. Can someone tell me if this behavior is consistent with >the ANSI standard ? It's not just IBM; many systems will line-buffer output to an interactive device, and the C standard encourages this. What you SHOULD have done is to insert fflush(stdout); before trying to read interactive input, to ensure that the previous prompt has been flushed out where the user can see it.
mcdaniel@adi.com (Tim McDaniel) (02/06/91)
ghot@s.ms.uky.edu (Allan Adler) notices that a printf prompt on his system does not come out until after the read, and wonders if ANSI C allows that. See the Frequently-Asked Questions list, question number 65. The full FAQ is posted at the beginning of each month, and an abbreviated version is posted in mid-month. E-mail me for a copy. (Also see questions 66 and 67 about the problems with using scanf for terminal input.) A brief answer: ANSI C allows it. The system in question uses the common mechanism of "line buffering", flushing the buffer at each newline. -- Tim McDaniel Applied Dynamics Int'l.; Ann Arbor, Michigan, USA Work phone: +1 313 973 1300 Home phone: +1 313 677 4386 Internet: mcdaniel@adi.com UUCP: {uunet,sharkey}!amara!mcdaniel
greywolf@unisoft.UUCP (The Grey Wolf) (02/06/91)
In article <1991Feb4.234251.14836@ms.uky.edu> ghot@s.ms.uky.edu (Allan Adler) writes: >Perhaps someone can clear up a mystery. The enclosed program compiles >correctly using IBMC on an IBM3090. When I run it, it refuses to execute >the first printf until after the user has typed something in for the >scanf statement, whereupon it executes both printf statements. (scanning ahead, the first printf() lacks a newline...) The standard i/o package uses buffering on standard output. This means that before anything is printed to the standard output, one buffer (or one line) worth of information must be sent there, depending upon how the file stream is buffered. Standard output is line buffered by default; hence the first printf doesn't see the light of day until it processes a newline (or until it is forcibly flushed -- see below). >In order to get the program to work correctly, the line >printf("Please type your name: "); >has to be replaced by >printf("Please type your name: \n"); Put an fflush(stdout) immediately after the first printf(). >I would never have predicted this based on my previous experience with C >and my readings. Can someone tell me if this behavior is consistent with >the ANSI standard ? The behaviour of [f]printf() is undefined if you are sending less than one bufferful of data to the associated file stream. [ program deleted for brevity ] See manual entries for fflush(3S), setbuf(3S). -- thought: I ain't so damb dumn! | Your brand new kernel just dump core on you war: Invalid argument | And fsck can't find root inode 2 | Don't worry -- be happy... ...!{ucbvax,acad,uunet,amdahl,pyramid}!unisoft!greywolf