Sams Teach Yourself C in 21 Days

(singke) #1
14: buffer[x++] = ch;
15:
16: buffer[x] = ‘\0’;
17:
18: printf(“%s”, buffer);
19:
20: return 0;
21: }

Here’s a string
Here’s a string

346 Day 14

LISTING14.5 continued

INPUT/
OUTPUT

Remember that getch()isn’t an ANSI-standard command. This means that
your compiler (and other compilers) might or might not support it. getch()
is supported by Symantec and Borland. Microsoft supports _getch(). If you
have problems using this command, you should check your compiler and see
whether it supports getch(). If you’re concerned about portability, you
should avoid non-ANSI functions. The Bloodshed Dev-C++ compiler included
with this book has support for getch(); however, its support is different
than described above—another issue with non-ANSI functions.

Caution


Thegetche()Function
This is a short section, because getche()is exactly like getch(), except that it echoes
each character to stdout. Modify the program in Listing 14.4 to use getche()instead of
getch(). When the program runs, each key you press is displayed on-screen twice—once
as echoed by getche(), and once as echoed by putchar().

getche()is not an ANSI-standard command, but many C compilers
Caution support it.

Thegetc()andfgetc()Functions
Thegetc()andfgetc()character input functions don’t automatically work with stdin.
Instead, they let the program specify the input stream. They are used primarily to read
characters from disk files. See Day 16 for more details.

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

Free download pdf