使用" \\ n"在用C scanf()的 [英] Using "\n" in scanf() in C
问题描述
我错误地使用了 scanf函数(%d个\\ n,&安培; VAL);
在我的节目之一,我无法理解的行为,功能表现。
I mistakenly used scanf("%d\n",&val);
in one of my programmes, I could not understand the behavior, the function showed.
int main(){
int val;
scanf("%d\n", &val);
printf("%d\n", val);
return 0;
}
现在程序所需的2整数的输入,并输出已输入的第一输入。
有什么区别应该就是多余的 \\ n
带来的?
Now the program required 2 integer inputs, and prints the first input that was entered.
What difference should that extra \n
brings?
我试图寻找,但未能找到答案,甚至通过 scanf函数
的手册。
I tried to search but couldn't find the answer, even through manual of scanf
.
推荐答案
这是的'\\ n'
- 或任何空白字符 - 在格式字符串占用整个(可能在输入空格字符空)序列。因此, scanf函数
,只有当遇到下一个非空白字符,或输入流的末尾(例如,当输入从文件重定向,并达到其最终返回后,或者在关闭标准输入用Ctrl-D)。
An '\n'
- or any whitespace character - in the format string consumes an entire (possibly empty) sequence of whitespace characters in the input. So the scanf
only returns when it encounters the next non-whitespace character, or the end of the input stream (e.g. when the input is redirected from a file and its end is reached, or after you closed stdin with Ctrl-D).
这篇关于使用" \\ n"在用C scanf()的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!