我刚刚在看经典的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/