TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: John Poltorak
from: Jonathan de Boyne Pollard
date: 1995-03-29 09:19:20
subject: scanf

JP>
  >  JP>         printf("Enter length: ");
  >  JP>         scanf("%d", &len);
  >
  >  JP> When I run this program, I don't get any prompts, before entering any
  >  JP> values. Is this just a quirk of using an OS/2 compiler, or is Mr. Schi
  >  JP> in error here?
JP>

  Mr Schildt is, as he is so often in fact, in error here.

  To quote the C standard :

       7.9.3  Files

          [...] When a stream is unbuffered, characters are intended to
          appear from the source or at the destination as soon as
          possible.  Otherwise characters may be accumulated and
          transmitted to or from the host environment as a block. When a
          stream is fully buffered, characters are intended to be
          transmitted to or from the host environment as a block when a
          buffer is filled.  When a stream is line buffered, characters
          are intended to be transmitted to or from the host environment
          as a block when a new-line character is encountered.

          [...] When opened, the standard error stream is not fully
          buffered; the standard input and output streams are fully
          buffered if and only if the stream can be determined not to
          refer to an interactive device.

JP>
  > Having just installed emx+gcc, I now get the expected results.
  > I am simply stunned to find ICC acting in such an erroneous way with
  > something so trivial. I also got unexpected results when
  > displaying numbers of type 'double'. All this makes me
  > rather hesitant about learning 'C' using the C Set/2
  > compiler.
JP>

  From the above, it can be seen that CSet is in fact correct.

  The ISO C standard only requires that the standard output stream *not*
  be *fully* buffered when it is determined to be connected to an
  interactive device.  It can quite legally be *line* buffered, which is
  exactly what it is in CSet.

  The simple (and obvious) solution is to use fflush(stdout) between
  every printf() that isn't terminated by '\n' and the following
  scanf().

  Another solution is to use setvbuf (q.v.) to switch it to being
  unbuffered.

  > JdeBP <
___
 X MegaMail 2.10 #0:

--- Maximus/2 2.02
* Origin: DoNoR/2,Woking UK (44-1483-722344) (2:440/4)
SEEN-BY: 105/42 620/243 711/401 409 410 413 430 807 808 809 934 955 712/407
SEEN-BY: 712/515 628 704 713/888 800/1 7877/2809
@PATH: 440/4 141/209 270/101 105/103 42 712/515 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.