From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f7344,b4a033983c1b721b X-Google-Attributes: gidf7344,public X-Google-Thread: 10f8a1,b4a033983c1b721b X-Google-Attributes: gid10f8a1,public X-Google-Thread: 135e0a,b4a033983c1b721b X-Google-Attributes: gid135e0a,public X-Google-Thread: f6134,b4a033983c1b721b X-Google-Attributes: gidf6134,public X-Google-Thread: 1164f8,b4a033983c1b721b X-Google-Attributes: gid1164f8,public X-Google-Thread: 114f7d,b4a033983c1b721b X-Google-Attributes: gid114f7d,public X-Google-Thread: 1014db,b4a033983c1b721b X-Google-Attributes: gid1014db,public From: bill@cafe.net (Kaz Kylheku) Subject: Re: scanf problem on Vax C vs Dec C Date: 1997/12/19 Message-ID: <67egcb$aa0$1@brie.direct.ca>#1/1 X-Deja-AN: 308643680 References: <3499185B.63ED5638@tle.enet.dec.com> <67eef5$7ei$1@thefuture.qualcomm.com> Reply-To: kaz@cafe.net Organization: Internet Direct Newsgroups: cern.vaxusers,comp.lang.c,comp.org.decus,comp.os.vms,comp.sys.dec,vmsnet.alpha,vmsnet.infosystems.misc In article <67eef5$7ei$1@thefuture.qualcomm.com>, Ryan Moore wrote: >Here's an example of a difference: > >#include > >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.