gpt4 book ai didi

c++ - C格式说明符问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:00 24 4
gpt4 key购买 nike

当我在工作时,我遇到了别人写的代码。 我看到一个声明,

sprintf(o_params->o_file_name,
"%s_%s_%04.4d_%s_%s.ASC",
"OUTD", "RM", sequence_no, DateStamp_buf1, TimeStamp_buf1
);

在上面的语句中,我看到了 %04.4d。这是正确的格式说明符吗?

变量 sequence_nostatic int 并且没有小数。

最佳答案

来自FreeBSD manpage man 3 打印

An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s conversions.

所以在这种情况下,%04.4d.4 指定应打印数字的所有四位数字。当然,如果小于 1000,04 部分只是用前导零填充数字。但是,在这种情况下,如上面的手册页所述,

`0' (zero) Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored.

既然肯定会打印所有四位数字,我猜这只是一个遗留问题或错字之类的。此语法使用 gcc -Wall 生成编译器警告(参见 Sinan Unur 的示例),但它似乎不是实际错误。

关于c++ - C格式说明符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1594582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com