user-0 提问: 关于scanf输入字符串的问题

回复:
user: user-1
content: 这样的问题到底存在的价值是什么？

user: user-2
content: 这个问题有讨论的必要吗

user: user-3
content: 因为回车键也是一个字符，所以加\n相当于一直在输出，不跳出来

user: user-4
content: 这里算是非输入控制符，最好不要这么写http://blog.csdn.net/u012421456/article/details/18501309

user: user-5
content: scanf("%d\n",&a);相当于scanf("%d ",&a); 其中的\n和空格匹配多个空白符，所以要直到出现非空白符scanf才返回，继续执行之后的语句

user: user-6
content: scanf("\n%d",&x);//输入的时候不是需要先输入\n嘛。要不不对的

user: user-7
content: 因为有缓冲区，加个 getchar();

user: user-8
content: 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.

user: user-9
content: 来自网上的答案： 用空白符结尾时，scanf会跳过空白符去读下一个字符，所以你必须再输入一个数;空白符包括空格，制表符，换行符，回车符和换页符,如果用scanf("%d  ",&a)也会出现同样问题  而就是说以空白符为结尾的话要在结尾输入一个非空白符的字符！

user: user-9
content: 引用 3 楼 u013491662 的回复:试了一下，好像是这么处理的 scanf函数的格式字符串，在格式声明前的\n都是作为空白符处理的，而格式声明之后的\n都是作为普通字符处理的  #include <stdio.h> int main() { 	int i,j; 	scanf("\n%d\n%d\n", &i,&j); 	printf("%d%d", i,j); 	return 0; }  比如说以上程序 要输入  1 2\n  才会处理，最后要输入两个字符"\n"而不是回车！ 错了错了，有试了一下 不是这么回事！

user: user-9
content: 试了一下，好像是这么处理的 scanf函数的格式字符串，在格式声明前的\n都是作为空白符处理的，而格式声明之后的\n都是作为普通字符处理的  #include <stdio.h> int main() { 	int i,j; 	scanf("\n%d\n%d\n", &i,&j); 	printf("%d%d", i,j); 	return 0; }  比如说以上程序 要输入  1 2\n  才会处理，最后要输入两个字符"\n"而不是回车！

user: user-10
content: 用gets吧

user: user-0
content: 刚刚试了一下，对于多个字符串的输入加\n的话就消除了回车键的影响