c - printf中 "."的含义

标签 c

我刚刚在看经典的K&R,遇到了如下语法:

printf("%.*s",max,s);

"." 在这里是什么意思?当我不在这里应用 "." 时,会打印整个字符串,但是当我们不应用时t apply a "." , atmost max characters are printed. 如果有人能解释一下,我将非常感激。

最佳答案

%.*s 中,.* 限制了将要写入的字节数。如果这是用包含的数字编写的,例如 %.34s,那么数字就是限制。使用星号时,限制取自 printf 的相应参数。

来自 C 2011 (N1570) 7.21.6.1 4,描述了 fprintf et al 的转换规范:

An optional precision that gives … the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero.

关于c - printf中 "."的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445396/

相关文章:

c - sizeof 如何处理数组指针的取消引用?

c - 我尝试在大学作业中使用 c 中的信号和暂停()函数,但无济于事

c - Redis 中 RedisModule_Alloc() 的自动内存管理

检查文件是否存在,给定该文件的目录。如何调试?

c - (C 编程) while 循环仅按特定顺序工作

c - 向字符串添加空终止符

c - 什么时候发生不可读内存页?

c - 简单的 C 程序没有存储变量中输入的正确值

c - gtk_container_get_children

c - 1个数组变成2个有malloc问题的字符串数组并通过ref传递