Sams Teach Yourself C in 21 Days

(singke) #1
Working with the Screen, Printer, and Keyboard 371

14


error_message(“An error has occurred.”);
void error_message(char *msg)
{
fprintf(stderr, msg);
}
By using your own function instead of directly calling fprintf(), you provide additional
flexibility (one of the advantages of structured programming). For example, in special
circumstances you might want a program’s error messages to go to the printer or a disk
file. All you need to do is modify the error_message()function so that the output is
sent to the desired destination.

Printer Output Under DOS and the Windows Command Prompt
On a DOS or Windows system, you send output to your printer by accessing the prede-
fined stream stdprn. On IBM PCs and compatibles, the stream stdprnis connected to
the device LPT1: (the first parallel printer port). Listing 14.15 presents a simple example.

To usestdprn, you need to turn ANSI compatibility off in your compiler.
Consult your compiler’s manuals for more information. Because stdprnis not
a part of the ANSI standard, your compiler may not support it.

Note


LISTING14.15 printer.c. sending output to the printer
1: /* Demonstrates printer output. */
2: /* stdprn is not ANSI standard */
3: #include <stdio.h>
4:
5: int main( void )
6: {
7: float f = 2.0134;
8:
9: fprintf(stdprn, “\nThis message is printed.\r\n”);
10: fprintf(stdprn, “And now some numbers:\r\n”);
11: fprintf(stdprn, “The square of %f is %f.”, f, f*f);
12:
13: /* Send a form feed. */
14: fprintf(stdprn, “\f”);
15:
16: return 0;
17: }

This message is printed.
And now some numbers:
The square of 2.013400 is 4.053780.

OUTPUT

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

Free download pdf