9)
Program 8 - instant input
Program 8 | Instant input | Source code - prog8.c |
Imagine playing a game where the 'a' key moved a spaceship left and the 's' key moved the spaceship right. If you used scanf for the user input, it would be a tiresome game, each time you wanted to move the spaceship you would have to press enter after pressing the 'a' and 's' keys. What is needed is a way that the program can detect a key press the instant it is pressed. The sample program below shows this way, using the getch statement. |
Program code- new parts shown in red |
|
Description of the program code | ||
The first big
change to this program from the others so far is the
inclusion of the line #include <conio.h>. This line is
needed because the program uses the 'C' getch();
function. If there are any programs that contain the
getch() function the conio.h header file has to be
included at the start of the program. Note : This isn't
required with Miracle-C, but other C compilers won't
compile the program if it's not included. Miracle-C
doesn't mind it being there though, so it is best to keep
it to retain compatibility with other C compilers. There is one variable defined for the program unsigned char k; This is a different data type than has been previously used. The reason an unsigned char is used is because the program uses it to store a "character" instead of a number. This isn't entirely true, as computers don't store characters ( a, b, c etc.. ), they only work with numbers, but each character has a unique code associated with it on all computers called an ASCII code. ASCII stands for American Standard Code for Information Interchange, and the codes and associated characters ( from 32 to 127 ) are shown in the table below. The full ASCII character set defines 256 codes and characters ( 0 - 255 inclusive ), but only codes 32 - 127 are of interest here. This means that computers refer to the letter J as number 74. It will be proved later if you don't believe it :-) With the range of possible ASCII codes being from 0 - 255 then an unsigned char data type is ideal for storing characters ( hence the name "char" ). Refer back to page 5 if you don't recall the minimum and maximum ranges of data types. The next line is where it all happens, k=getch(); When the program reaches this point it will wait for the user to press a key. The instant it detects a key being pressed by the user it will store the ASCII code of the corresponding key character in the k variable. It will not wait for the enter key to be pressed this time though, but continue on to the next line as soon as the key is pressed by the user. The empty brackets () have to be included at the end of the getch function, when functions are covered soon, it will be explained why. The final line of the program printf("You pressed the %c key",k); is the the same as previous programs apart from the use of the %c format specifier instead of %d. The use of the %c format specifier tells not to display the decimal value of the variable k, but to display the ASCII character associated with the value held in the variable k. It is impossible to show a screen shot of the program without knowing what the user pressed, assuming the user pressed the 'p' key on the keyboard and didn't have the CAPS LOCK on, the following is displayed.
You are probably thinking that's pretty obvious, but if you change the last line of the program to read printf("You pressed the %d key",k); so that the %c is changed to %d, a different output is displayed.
If you look at the ASCII character set below, you can see that the ASCII code for a small case 'p' is 112. This is the proof that what is being stored in the variable k is not the letter p, but the number 112. The use of the %c format specifier in the printf statement tells the program not to display the numeric value, but the associated ASCII character, but when it is changed to %d it displays the numeric value of the variable k. To jazz up the program a bit more you could change the printf line to printf("You pressed the %c key, which is ASCII code %d",k,k); |
ASCII character set - codes 32 - 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Summary |
This page contained
a lot of information for such a small program, and
covered quite a bit. The ASCII table above is useful for
reference, although with a quick search on the net you
should be able to find the full set of all 255 character
codes. The main thing to take from this page is the fact
that computers don't store characters, they always store
numbers. You can program in the functionality to display
the stored numbers as characters though, as was shown
with the %c format specifier. If you look at the ASCII
character set above, you can see that both the upper and
lower case letters have a different ASCII code, which
becomes important later on. There is enough information now to go on to produce a simple menu system and introduce the if statement on the next page. |
Tasks |
8.1) Crack this code : 70, 105, 115, 104, 115, 105, 109 |
(c) J.C.Spooner 2001