March 15 C
Read chapter 5. Switch and Cases, interesting....
Jim says
Here's the variation I showed in class,
thinking about getchar, ascii, and ints.
#include <stdio.h>
int main(){
int character;
int number;
printf("type some characters: ");
character = getchar();
printf("The first one you typed was '%c' \n", character);
if (character == 'a'){
printf("that was an 'a'\n");
}
else {
printf("that NOT was an 'a'\n");
}
character = getchar();
number = character - '0';
printf("The second char you typed was '%c' which is ascii %i (or 0x%x). If that was a digit, the digit is %i. \n",
character, character, character, number);
return 0;
}
Running it gives
$ gcc test.c -o test
$ ./test
type some characters: a3
The first one you typed was 'a'
that was an 'a'
The second char you typed was '3' which is ascii 51 (or 0x33).
If that was a digit, the digit is 3.