printf("enter the string");
scanf("%[^\t\n]s",str);
//abv 代码如何在不使用 gets 且仅使用 scanf 的情况下接受多字字符串
最佳答案
[
Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating null byte. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket[
character and a close bracket]
character. The set excludes those characters if the first character after the open bracket is a circumflex (^
)....
The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.
(Linux scanf(3)
联机帮助页。)
因此 scanf
会读取第一个不是制表符 (\t
) 或换行符 (\n
) 的字符。它确实读取空格。
关于c - 这是如何使用 scanf 接受多字字符串的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4977467/