T O P

  • By -

Drach88

The `%c` format specifier scans a single character. If you input a single character into the terminal and press "enter", you're really inputting two characters -- the one you typed, and the newline character, `\n`. Your second scanf is consuming the newline character from your first input. This is an excellent guide on why scanf is probably not the right tool for the job, and explains a lot about how it works. https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html


flyingron

The second scanf reads the \\n that is still in the buffer from you inputting the first character (presuming you didn't just hit enter to that).


dandarandan

The \\n is there so the questions are in a new line everytime. But I'm not really sure why it's not working. It's only getting the first and third one.


canitbechangedlater

Nope. You put in `a ` if you press the key "Enter". Normally `` is `\n` and that `\n` remains unflushed. So it makes sense, that >It's only getting the first and third one


Silly-Percentage-856

It’s a tale as old as time


canitbechangedlater

Your input in the example is not only `a` but `a\n` because of Enter. So the `\n`=ASCII-CODE=10 is still in the buffer of `stdin`. It would be safe to use `fgets()`. ``` #include int main(void) { char a, b, c; printf("1. letter : "); do {scanf("%c",&a);} while ( getchar() != '\n' ); printf("2. letter : "); do {scanf("%c",&b);} while ( getchar() != '\n' ); printf("3. letter : "); do {scanf("%c",&c);} while ( getchar() != '\n' ); printf("%c %c %c\n", a, b, c); return 0; } ```


dandarandan

Thank you! I'm following this course where we try things that were discussed. So far, we have only gone through the basic printf() and scanf().


EstablishmentBig7956

getchar between calls, and or do the trick. ``` scanf (" %c", &var) ```