我应该如何打印类型,如off_t和为size_t? [英] How should I print types like off_t and size_t?
问题描述
我试图打印类型,如 off_t
和为size_t
。什么是的printf正确的占位符()
的就是便携的?
I'm trying to print types like off_t
and size_t
. What is the correct placeholder for printf()
that is portable?
或者是有一个完全不同的方式来打印这些变量?
Or is there a completely different way to print those variables?
推荐答案
您可以使用以Z
用于为size_t和 T
为ptrdiff_t的像
You can use z
for size_t and t
for ptrdiff_t like in
printf("%zu %zd", size, ptrdiff);
但我的联机帮助说一些老图书馆采用了不同性格比以Z
和鼓励使用它。尽管如此,它的标准化(由C99标准)。对于那些还会将intmax_t
和中int8_t
stdint.h
的等对,还有你可以使用宏,像另一个回答说:
But my manpage says some older library used a different character than z
and discourages use of it. Nevertheless, it's standardized (by the C99 standard). For those intmax_t
and int8_t
of stdint.h
and so on, there are macros you can use, like another answer said:
printf("value: %" PRId32, some_int32_t);
printf("value: %" PRIu16, some_uint16_t);
他们是在的手册页所列inttypes.h
。
就个人而言,我只想投的价值观无符号长
或长
像另一个答案建议。如果你使用的C99,那么你可以(而且应该,当然)转换为无符号长长
或长长
和使用%LLU
或%LLD
分别格式。
Personally, I would just cast the values to unsigned long
or long
like another answer recommends. If you use C99, then you can (and should, of course) cast to unsigned long long
or long long
and use the %llu
or %lld
formats respectively.
这篇关于我应该如何打印类型,如off_t和为size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!