Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!think.com!linus!agate!ucbvax!unisoft!greywolf From: greywolf@unisoft.UUCP (The Grey Wolf) Newsgroups: comp.lang.c Subject: Re: printf and scanf in IBMC on IBM 3090 Message-ID: <3346@unisoft.UUCP> Date: 6 Feb 91 02:05:04 GMT References: <1991Feb4.234251.14836@ms.uky.edu> Reply-To: greywolf@unisoft.UUCP (The Grey Wolf) Organization: Foo Bar and Grill Lines: 37 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