Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 10: Cook That C Variable Charred, Please 125

Reading and Writing Single Characters

This book introduces you to two C language functions used to read text from the keyboard: scanf() and gets(). Both read text from the keyboard, usually as full sentences. However, scanf() can be used to read in single characters, which is part of its charm — and flexibility:

scanf(“%c”,&key);

In this format, scanf() reads the keyboard for a single character, specified by the placeholder %c. That value is stored in the char variable key, which you can assume was already declared earlier in the program. To wit:

#include <stdio.h>

int main()

{

char key;

puts(“Type your favorite keyboard character:”); scanf(“%c”,&key);

printf(“Your favorite character is %c!\n”,key); return(0);

}

Carefully enter this source code into your editor. Save it to disk as FAVKEY1.C.

Compile and run. Here’s the sample output:

Type your favorite keyboard character:

Press a key, such as m (or whatever your favorite is), and then press the Enter key. You see this:

M

Your favorite character is M!

The M key is read from the keyboard, stored in the char variable key, and then displayed by printf().

Yes, you have to press the Enter key to finish your input — that’s the way the scanf() function works.

You can type a whole string of text, but only the first character that’s typed is read by scanf() as the favorite key.

126 Part II: Run and Scream from Variables and Math

The getchar() function

Fortunately, you’re not stuck with scanf() for reading in individual keys from the keyboard. The C language also has a function named getchar(), which is used specifically to read a single character from the keyboard. Here’s the format:

var=getchar();

var is a character variable. It holds whatever character is typed at the key­ board. var is followed by an equal sign and then getchar and two parenthe­ ses hugging nothing. This function is a complete statement and ends with a semicolon.

The getchar() function causes your program to pause and wait for a key to be typed at the keyboard. getchar() sits and waits. Sits and waits. Sit. Wait. Sit. Wait. When a key is typed and then Enter is pressed, that character’s “value” slides across the equal sign and is stored in the character variable.

The following is the update to the FAVKEY1.C program, this time replacing the sordid scanf() function with the better getchar() function:

#include <stdio.h>

int main()

{

char key;

puts(“Type your favorite keyboard character:”); key=getchar();

printf(“Your favorite character is %c!\n”,key); return(0);

}

Edit the source code for FAVKEY1.C, changing only Line 8 and replacing the scanf() function with getchar(), as just shown. Save the new file to disk as FAVKEY2.C. Compile and run!

The output is the same as for the first version of the program; and you still have to press the Enter key to enter your favorite key value.

Yes, it seems silly that you have to type Enter when entering a single char­ acter. That’s just the way getchar() works. (And I hate it.)

There are ways to read the keyboard more interactively, where pressing the Enter key isn’t required. I cover these methods in my book C All-in- One Desk Reference For Dummies (Wiley).

Chapter 10: Cook That C Variable Charred, Please 127

The putchar() function

The putchar() function, pronounced “püt-care,” for “put character,” is the opposite of the getchar() function; getchar() reads in a character from the keyboard, and putchar() displays a character on the screen. Here’s the format, though you probably could have guessed it:

putchar(c);

c can be either a single-character variable or a character constant in single quotes:

putchar(‘c’);

The character ‘c’ is displayed on the screen.

The following program shows how putchar() can be put to use in tossing up characters on the screen:

#include <stdio.h>

int main()

{

puts(“Press Enter:”); getchar(); putchar(‘H’); putchar(‘e’); putchar(‘l’); putchar(‘l’); putchar(‘o’); putchar(‘!’); putchar(‘\n’); return(0);

}

Enter this source code as PUTHELLO.C. Double-check for errors. Compile and run:

Press Enter.

Do so. Then:

Hello!

Note that getchar() is used here merely to read the Enter key press; any value returned by getchar() isn’t stored. When used in this format, getchar() still returns a value, but because it’s not stored in a variable, the value is “lost.” That’s perfectly okay.