printf and scanf in IBMC on IBM 3090

The Grey Wolf greywolf at unisoft.UUCP
Wed Feb 6 13:05:04 AEST 1991


In article <1991Feb4.234251.14836 at ms.uky.edu> ghot at 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



More information about the Comp.lang.c mailing list