C 格式说明符
当我工作时,在代码中的某个地方我看到了以下语句。 我对 sprintf 中的格式说明符感到困惑,
d_number = strtol( tmp_buf , (char **)NULL, 16);
memset( tmp_buf , ' ' , sizeof( tmp_buf ) );
sprintf( tmp_buf , "%0.*d" , (int)sizeof( dec_number ) , d_number );
有人可以解释一下吗?
While i am working ,somewhere inside the code i saw the following staements.
I am getting confused by the format specifier in sprintf
d_number = strtol( tmp_buf , (char **)NULL, 16);
memset( tmp_buf , ' ' , sizeof( tmp_buf ) );
sprintf( tmp_buf , "%0.*d" , (int)sizeof( dec_number ) , d_number );
could anybody explain please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.*
表示精度未在格式字符串中指定,而是作为必须格式化的参数之前的附加整数值参数。 (d_number)http://www.cplusplus.com/reference/clibrary/cstdio/ printf/
.*
means the precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. (d_number)http://www.cplusplus.com/reference/clibrary/cstdio/printf/
* 被替换为 (int)sizeof(dec_number)。如果 dec_number 是 int,则在许多机器上它与 %0.4d 相同。这是打印整数的精度或要打印的最大位数。
* 可以出现在“.”的两侧,例如:
也可以。
The * is replaced by (int)sizeof(dec_number). If dec_number is an int, on many machines it is the same as %0.4d. This is the precision with which to print the integer or the maximum number of digits to print.
The * can appear on either side of the ".", for example:
also works.