scanf reading “Enter” key

后端 未结 3 1706
既然无缘
既然无缘 2021-01-03 14:23

Why scanf doesn\'t work when I type \"Enter\" in the code below?

#include 
#include 
#include 

int main(int a         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 15:15

    Scanf looks through the input buffer for the specified format, which is string in this case. This has the effect of skipping your whitespaces. If you put a space between wording, it skips the space looking for the next string, similarly it will skip tabs, newlines etc. See what happens if you put a %c instead. It will pick up the newline because it is searching for a char now, and '\n' constitutes as a valid char.

    If you want the same effect while getting whitespace, change it to a %c and remove the newline escape character at the print statement.

提交回复
热议问题