C: printf 中的 ptrdiff_t 应该使用哪个字符?
printf
中的 ptrdiff_t
应该使用哪个字符?
C标准是否清楚地解释了如何在printf
中打印ptrdiff_t
?我还没有找到任何人。
int a = 1;
int b = 2;
int* pa = &a;
int* pb = &b;
ptrdiff_t diff = b - a;
printf("diff = %?", diff); // % what?
Which character should be used for ptrdiff_t
in printf
?
Does C standard clearly explains how to print ptrdiff_t
in printf
? I haven't found any one.
int a = 1;
int b = 2;
int* pa = &a;
int* pb = &b;
ptrdiff_t diff = b - a;
printf("diff = %?", diff); // % what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
%td
,如果您的编译器不支持它,您应该尝试%ld
(也将输入转换为long
)。Use
%td
and if your compiler does not support it, you should try%ld
(also cast the input tolong
).这是
%td
。请参阅此处或更好的此处。It's
%td
. See here or better still here.C11 草案解释了 7.21.6.1 7“
fprintf
函数”中ptrdiff_t
的长度修饰符使用
"%td"
如下: Credit: @trojanfoe如果编译器不 < strong>不支持
"%td"
,转换为有符号类型 - 越长越好。然后确保替代格式和参数匹配。参考格式说明符
C11 draft explains the length modifier for
ptrdiff_t
in 7.21.6.1 7 "Thefprintf
function"Use
"%td"
as in the following: Credit: @trojanfoeIf the compiler does not support
"%td"
, cast to a signed type - the longer, the better. Then insure the alternative format and argument match.Ref format specifiers