C 编程中的scanf("%s")、scanf(\xe2\x80\x9d%[^\\n]s")和有什么区别?gets(a)
scanf("%[^\\n]s", a)\n\nscanf("%s", a)\n\ngets(a)\nRun Code Online (Sandbox Code Playgroud)\n\n三种字符数组输入方式的主要区别是什么?
\nscanf("%s",a);将跳过输入中的前导空白字符,并匹配字符直到遇到空白字符(或到达文件结尾),将它们存储在参数指示的数组中。请注意,这很容易受到缓冲区溢出的影响,因此最好包含一个宽度说明符,该说明符提供从输入读取的最大字符数。例如,如果a是一个100的数组char,%99s应该使用;\0这为由自动添加的终止符留下了空间scanf()。
Neither scanf("%s[^\n]",a); nor scanf("%[^\n]s", a); is probably what was meant, and instead this should be: scanf("%[^\n]",a);. The %[] is a scanset directive. There is no need to follow the %[] directive with an s, which would only tell scanf() to match a literal s in the input after finishing with %[]. The scanset directive matches characters described within the brackets and assigns them to the corresponding argument. When a match fails, that character is placed back in the input stream. Here, the ^\n indicates that all characters except for newline characters should be matched, so this directive will match characters until a newline is encountered, and the newline will remain in the input stream. The same advice about specifying a maximum width applies here as well: %99[^\n] to avoid buffer overflow, if a is an array of 100 chars. Note that the %[^\n] directive will match any character that is not a \n, including other whitespace characters. This means that it will not skip over leading whitespace characters (but a leading \n in the input will cause the directive to immediately fail, without making an assignment), in contrast to %s, and will read lines of input containing spaces.
puts(a); does not read input, but is an output function. Note that this function prints a newline after printing its argument. Perhaps you meant to include gets() in this list of methods to gather input.
gets(a); is an unsafe function that was deprecated in C99, and completely removed from the language in C11. You should never use this function for any reason. This function fetches a line of input, reading characters until a newline character is encountered, or until end-of-file is reached. The newline character is discarded; it is not stored in the array indicated by a, and it is not returned to the input stream. For this reason, when gets() was used in the past, it worked well with puts(), which automatically prints a newline character after printing its argument.
Finally, for a little more information, there is fgets(). This function fetches a line of input, but takes a size argument so that buffer overflow may be avoided. Given my earlier example of char a[100];, fgets() would be called like this:
fgets(a, 100, stdin);
Run Code Online (Sandbox Code Playgroud)
or sometimes:
fgets(a, sizeof a, stdin);
Run Code Online (Sandbox Code Playgroud)
Here, fgets() reads at most one character less than the number specified by the size argument, allowing space for a \0, which is always added. If the \n is read, it is stored in a. Since the newline is not discarded, puts() does not work as well here; often the newline needs to be removed after taking input with fgets().