think about the byte stream and how it works. getchar() returns a character at a time. so when it returns the X or O, there is still the newline character behind it in the stream. so the inner while loop fails on O or X, and then the outer loop continues, and the next read from stdin produces the '\n' character, so it outputs the second prompt. so when you read an X or O, you'll have to clear the input buffer.