在scanf函数中,回车键不是代表,开始匹配,开始读取缓冲区中的数据吗?
本人不是大佬,不懂编译的底层原理,就单纯说说自己的看法
总结,能这样思考钻研确实是一件非常好的事情,但对于细枝末节的事情,以砸门的程度,不必过于最牛角尖,知道砸门用就行。
printf("%d", printf("%d",20221101));
又是新一月的开始,注意身体,早点休息~
https://en.cppreference.com/w/c/io/fscanf
- non-whitespace multibyte characters except %: each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal.
- whitespace characters: any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling isspace in a loop). Note that there is no difference between "\n", " ", "\t\t", or other whitespace in the format string.
- All conversion specifiers other than [, c, and n consume and discard all leading whitespace characters (determined as if by calling isspace) before attempting to parse the input.
scanf 中%d后面也没有必要加\n,因为在 scanf 中\n不起换行的作用。它不但什么作用都没有,你还要原样将它输入一遍。
看这篇文章非常非常详细,一定能解决你的一些问题。
《C语言scanf函数用法完全攻略》, 一起来围观吧 https://blog.csdn.net/weixin_43025071/article/details/98959744?utm_source=app&app_version=5.0.1&code=app_1562916241&uLinkId=usr1mkqgl919blen
实际上第一种写法%d%d最好,第二种要实际和scanf内容输入一样才能被读取,拿第一种来说
scanf读取数字字符时一开始缓存区读取如果是间隔字符回车空格或者tab键时会跳过并释放,直到读取到数字字符后开始依次往后读取,直到遇到间隔符会讲读取到的数字读初缓存区,此时第一个%d读取到了按回车前的数字,然后再进入缓存区读取数字字符给第二个数字,刚进缓存区遇到的是读取的前面数字字符按的回车会跳过并释放,期间你按多少回车空格tab都是会跳过释放的,知道你输入了数字最后按回车会读取这些数字直到间隔符后停止讲这些数字字符转换为数字给第二个变量,注意期间如果是非数字字符也会出缓存区,但是读取数字字符时遇到非数字字符会停在那里既不会跳过也不会被释放