Using Characters:
one more improvement

Another improvement we can make to the guessing game program is to make the player interface more natural. When we ask the player if they want to continue, they must enter 0 or 1 to answer. It might be better to allow them to enter y or n.

But 'y' and 'n' are not integers - they are characters entered from the keyboard. How do we store characters?

The char data type

C provides another primitive data type called char, and we can declare variabels of this type:
     char ch;

This declaration allocates a cell that can hold exactly one character:
ch
We can assign characters to this cell using character constants:
     ch = 'y';

ch
We can also read a single character from the input by using the %c conversion specifier in the format string given to scanf():
     scanf("%c", &ch);

We could then compare the character read:
     if( ch == 'y' )  keep_playing = TRUE;
     else keep_playing = FALSE;

The modified code is in guess.c.

So, what exactly are these characters?


[up] to Overview.