user-0 提问: scanf的问题

回复:
user: user-1
content: #include <stdio.h> char s[]="123 ab 4"; char *p; int v,n,k; void main() {     p=s;     while (1) {         k=sscanf(p,"%d%n",&v,&n);         printf("k,v,n=%d,%d,%d\n",k,v,n);         if (1==k) {             p+=n;         } else if (0==k) {             printf("skip char[%c]\n",p[0]);             p++;         } else {//EOF==k             break;         }     }     printf("End.\n"); } //k,v,n=1,123,3 //k,v,n=0,123,3 //skip char[ ] //k,v,n=0,123,3 //skip char[a] //k,v,n=0,123,3 //skip char[b] //k,v,n=1,4,2 //k,v,n=-1,4,2 //End.

user: user-1
content: 作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。

user: user-0
content: 引用 6 楼 czarten 的回复:最后一个问题，为什么只有第一次scanf需要输入两次，后面的都不需要呢，我给你演示一下过程你就懂了： 引用 8 楼 zhao4zhong1 的回复:http://bbs.csdn.net/topics/390817373  喃俩是被我的结贴速度感动了吗  我都看了，谢谢  确实scanf用到的时候不多，才注意这个问题  英语也是硬伤

user: user-1
content: http://bbs.csdn.net/topics/390817373

user: user-1
content: White-space characters: blank (' '); tab ('\t'); or newline ('\n').  空白字符包括：空格(' ');制表符('\t');或换行符('\n').  A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character.   一个空白字符导致scanf从输入缓冲区中读入所有后续的空白符直到遇到非空白符为止，且不保存。  One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. 在格式符中的一个空白字符匹配输入缓冲区中的任意个（包括0个）空白符以及空白符的任意组合。  英语也是一门计算机语言的说。  2楼内容可以作为计算机专业英语英译汉的典型练习题。

user: user-2
content: 最后一个问题，为什么只有第一次scanf需要输入两次，后面的都不需要呢，我给你演示一下过程你就懂了：  首先我们系统里有个键盘缓冲区，键盘上按下的字符都送到缓冲区里，scanf也是从缓冲区里读取，这个不清楚你知不知道  一开始，缓冲区是空的，scanf("%d\n")等着你输入第一个数  你输入：１，回车 １进入缓冲区，被%d吃掉，回车进入缓冲区，被\n吃掉，scanf等着你结束输入  你输入：２，回车 ２进入缓冲区，scanf发现不是它要的，忽略，回车进入缓冲区，scanf看到回车，知道输入结束了  接下来是第二次scanf("%d\n") 注意此时缓冲区不是空的，里面有你刚才敲进去的2和回车！于是２和回车直接被%d和\n读取，scanf等你结束输入 你不知道scanf这小子已经把该读的都读完了，以为它在等你输入，于是敲了３和回车 同样，３和回车进入缓冲区，scanf看到回车结束输入，而这个３和回车留给了下一次scanf  所以，你第一次输入的两个数，实际上给了第一个和第二个变量 第二次输入的数，实际上给了第三个变量 不信你可以输出来看看，是不是错位的

user: user-2
content: 我来给你说吧。  scanf("%d\n",&pos1[ii]); 这句scanf不该加那个\n，否则你输入完后，按下回车，这个回车就被\n匹配了，而不被认为是结束输入，所以系统继续等着你结束输入，你要再来一个回车，才会按结束输入处理。  如果你去试试，第一次输入的时候，输入一个数，按两次回车，会发现仍然不行，系统依旧等着你输入 这个是因为，一个\n可以匹配连续的多个换行，类似于scanf("%d %d",&a,&b)这句话，两个数中间有个空格，但是你输入两个空格三个空格都行，都只当做一个空格，同理这里的\n不管你连续按多少次回车，都会被这个\n吃掉  而你再输入一个数后再按回车，就输入成功了，这是因为，刚才我说了，一个\n是匹配连续的多个换行，你输入了别的东西，再来一个换行就不是连续的了，就不会被\n吃掉，就会作为输入结束符处理

user: user-1
content: 1楼已经解释的很清楚了。

user: user-0
content: 把scanf里的换行去掉则正常，这个怎么解释

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:scanf("%d",&pos1[ii]);  请检查scanf的返回值。   输入的第一串字符scanf没有返回

user: user-1
content: scanf("%d",&pos1[ii]);  请检查scanf的返回值。  Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.