c - 这是如何使用 scanf 接受多字字符串的?

标签 c

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/

相关文章:

c - fork() 和二叉树创建

c - 读取文件并填充结构

c - 编译错误,但提到了所需的库

c - 在 C 中处理指向 2D 数组的指针时出现不稳定的行为

c - Big-O 用于使用 for 循环插入 AVL

c - 如何从 C 中的 MACRO 函数返回一个字符串?

c - 如何检查void指针是否指向任意低地址?

c - for 循环内计数器的奇怪行为

c - libxml2属性修改C

c - 在 C 中使用 ADT 时发出 GNU 不兼容指针类型警告