c - : in scanf?的目的是什么

标签 c

scanf("%d:%d:%d%s", &hh, &mm, &ss, t12)

当在显示时间上采用多个输入时,输入按上面的方式编写,其中:在输入语句中使用,上面的行工作正常,但有人可以解释输入语句中冒号的需要和用法

最佳答案

根据标准,C11 7.21.6.2 fscanf 函数/3 和/6:

The format is composed of zero or more directives: one or more white-space characters, an ordinary multibyte character (neither % nor a white-space character), or a conversion specification.

A directive that is an ordinary multibyte character is executed by reading the next characters of the stream. If any of those characters differ from the ones composing the directive, the directive fails and the differing and subsequent characters remain unread.

因此 : 只是表示“确保流中的下一个字符是冒号”。不多不少。

您的格式字符串仅意味着您将能够扫描诸如12:34:56am之类的内容 - 如果格式字符串中没有文字冒号,则扫描将失败.

关于c - : in scanf?的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998399/

相关文章:

c - 如何在迭代字符串指针后从 void 函数获取指针

c - 如何理解4.4BSD的ip头定义?

c - 关于pthread_cond_wait的用法

c - case 语句 c 中的 if 语句似乎忽略条件而只执行 else

c - 将新节点添加到列表时出现段错误

c - 监听两个不同套接字接口(interface)的服务器程序

c - 静态数组的二维数组索引失败

c - 数组到指针的转换是否消除了间接运算符的评估?

c - 从文件读取单词时出现段错误

c - 将 m5ops.h 链接到 C 项目