// this program prints the ASCII code for different characters // that the user inputs. The program exits when the user enters // the character 'x'. #include int main() { while(1) { char c; // fflush removes the character that remains to read, after the user // has entered a character. Also, if the user enters more than one // character in a line, fflush discards all characters except for the // first one. // To experiment, compare with what happens // when you comment out the fflush line. // note: this trick does not work with g++ (if you use Visual C++, // ignore this note). fflush(stdin); printf("enter a character: "); scanf("%c", &c); printf("the number corresponding to %c is %d\n\n", c, (int) c); if (c == 'x') { break; } } }