Sams Teach Yourself C in 21 Days

(singke) #1
TABLE14.1 The five standard streams
Name Streams Device
stdin Standard input Keyboard
stdout Standard output Screen
stderr Standard error Screen
stdprn* Standard printer Printer (LPT1:)
stdaux* Standard auxiliary Serial port (COM1:)
*Supported only under Windows and DOS—Not part of ANSI standard.

You have actually been using two of these streams already. Whenever you have used the
printf()orputs()functions to display text on-screen, you have used the stdout
stream. Likewise, when you use gets()orscanf()to read keyboard input, you use the
stdinstream. The standard streams are opened automatically, but other streams, such as
those used to manipulate information stored on disk, must be opened explicitly. You’ll
learn how to do this on Day 16. The remainder of this chapter deals with the standard
streams.

Using C’s Stream Functions ..............................................................................


The C standard library has a variety of functions that deal with stream input and output.
Most of these functions come in two varieties: one that always uses one of the standard
streams, and one that requires the programmer to specify the stream. These functions are
listed in Table 14.2. This table doesn’t list all of C’s input/output functions, nor are all of
the functions in the table covered in today’s lesson.

TABLE14.2 The standard library’s stream input/output functions
Uses One of the Requires a
Standard Streams Stream Name Description
printf() fprintf() Formatted output
vprintf() vfprintf() Formatted output with a variable argument list
puts() fputs() String output
putchar() putc(),fputc() Character output
scanf() fscanf() Formatted input
vscanf() vfscanf() Formatted input with a variable argument list
gets() fgets() String input
getchar() getc(),fgetc() Character input
perror() String output to stderronly

340 Day 14

22 448201x-CH14 8/13/02 11:12 AM Page 340

Free download pdf