Sams Teach Yourself C in 21 Days

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

14


This function writes the character stored in ctostdout. Although the prototype specifies
a type intargument, you pass putchar()a type char. You can also pass it a type intas
long as its value is appropriate for a character (that is, in the range 0 to 255). The func-
tion returns the character that was just written, or EOFif an error has occurred.
You saw putchar()demonstrated in Listing 14.2. Listing 14.10 displays the characters
with ASCII values between 14 and 127.

LISTING14.10 putchar.c. Theputchar()function
1: /* Demonstrates putchar(). */
2:
3: #include <stdio.h>
4: int main( void )
5: {
6: int count;
7:
8: for (count = 14; count < 128; )
9: putchar(count++);
10:
11: return 0;
12: }

?¤????¶§?????????? !”#$%&’()*+,-./0123456789:;<=>?@ABCDEF
GHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
You can also display strings with the putchar()function (as shown in Listing 14.11),
although other functions are better suited for this purpose.

LISTING14.11 putchar2.c. Displaying a string with putchar()
1: /* Using putchar() to display strings. */
2:
3: #include <stdio.h>
4:
5: #define MAXSTRING 80
6:
7: char message[] = “Displayed with putchar().”;
8: int main( void )
9: {
10: int count;
11:
12: for (count = 0; count < MAXSTRING; count++)
13: {
14:
15: /* Look for the end of the string. When it’s found, */
16: /* write a newline character and exit the loop. */

OUTPUT

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

Free download pdf