如何打印off_t和size_t等类型?

时间:2022-09-06 20:06:16

I'm trying to print types like off_t and size_t. What is the correct placeholder for printf() that is portable?

我在尝试打印off_t和size_t之类的类型。什么是可移植的printf()的正确占位符?

Or is there a completely different way to print those variables?

还是有一种完全不同的方式来打印这些变量?

10 个解决方案

#1


87  

You can use z for size_t and t for ptrdiff_t like in

你可以用z表示size_t,而t表示ptrdiff_t。

printf("%zu %zd", size, ptrdiff);

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:

但我的手册上说,一些旧图书馆使用的字符与z不同,并且不鼓励使用它。然而,它是标准化的(C99标准)。对于那些intmax_t和int8_t的stdint。h等等,有一些宏你可以用,像另一个答案说:

printf("value: %" PRId32, some_int32_t);
printf("value: %" PRIu16, some_uint16_t);

They are listed in the manpage of inttypes.h.

它们列在inttypes.h的manpage上。

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.

就我个人而言,我只需要将这些值转换成像另一个答案所建议的那样长或长。如果您使用C99,那么您可以(当然也应该)将其转换为unsigned long long或long long,并分别使用%llu或%lld格式。

#2


85  

To print off_t:

打印off_t:

printf("%jd\n", (intmax_t)x);

To print size_t:

打印size_t:

printf("%zu\n", x);

To print ssize_t:

打印ssize_t:

printf("%zd\n", x);

See 7.19.6.1/7 in the C99 standard, or the more convenient POSIX documentation of formatting codes:

在C99标准中见7.19.6.1/7,或更方便的格式代码POSIX文档:

http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html

http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html

If your implementation doesn't support those formatting codes (for example because you're on C89), then you have a bit of a problem since AFAIK there aren't integer types in C89 that have formatting codes and are guaranteed to be as big as these types. So you need to do something implementation-specific.

如果您的实现不支持那些格式代码(例如,因为您在C89上),那么您就有一点问题,因为在C89中没有整数类型,它有格式化代码,并且保证会像这些类型一样大。因此,您需要执行特定于实现的操作。

For example if your compiler has long long and your standard library supports %lld, you can confidently expect that will serve in place of intmax_t. But if it doesn't, you'll have to fall back to long, which would fail on some other implementations because it's too small.

例如,如果您的编译器有很长的时间,并且您的标准库支持%lld,那么您可以很自信地期望它可以代替intmax_t。但是如果不这样做,您将不得不退回到long,这将在其他一些实现上失败,因为它太小了。

#3


5  

For Microsoft, the answer is different. VS2013 is largely C99 compliant but "[t]he hh, j, z, and t length prefixes are not supported." For size_t "that is, unsigned __int32 on 32-bit platforms, unsigned __int64 on 64-bit platforms" use prefix I (capital eye) with type specifier o, u, x, or X. See VS2013 Size specification

对于微软来说,答案是不同的。VS2013基本上是C99兼容的,但“他hh、j、z和t长度前缀不受支持”。对于size_t“也就是说,在32位平台上没有签名的__int32,在64位平台上没有签名的__int64”使用前缀I (capital eye)和类型说明符o、u、x或x。

As for off_t, it is defined as long in VC\include\sys\types.h.

至于off_t,它在VC中定义的很长,包括\sys\types.h。

#4


3  

Which version of C are you using?

你用的是哪个版本的C ?

In C90, the standard practice is to cast to signed or unsigned long, as appropriate, and print accordingly. I've seen %z for size_t, but Harbison and Steele don't mention it under printf(), and in any case that wouldn't help you with ptrdiff_t or whatever.

在C90中,标准的做法是将签名或未签名的long类型转换为合适的,并相应地打印。我已经看到了size_t的%z,但是Harbison和Steele在printf()中没有提到它,在任何情况下都不能帮助您使用ptrdiff_t或其他任何东西。

In C99, the various _t types come with their own printf macros, so something like "Size is " FOO " bytes." I don't know details, but that's part of a fairly large numeric format include file.

在C99中,各种_t类型都有自己的printf宏,所以像“Size”这样的东西是FOO“字节”。我不知道细节,但这是一个相当大的数字格式的一部分,包括文件。

#5


3  

You'll want to use the formatting macros from inttypes.h.

您需要使用inttypes.h中的格式宏。

See this question: Cross platform format string for variables of type size_t?

请参见这个问题:类型size_t的变量的跨平台格式字符串?

#6


1  

Looking at man 3 printf on Linux, OS X, and OpenBSD all show support for %z for size_t and %t for ptrdiff_t (for C99), but none of those mention off_t. Suggestions in the wild usually offer up the %u conversion for off_t, which is "correct enough" as far as I can tell (both unsigned int and off_t vary identically between 64-bit and 32-bit systems).

在Linux、OS X和OpenBSD上查看man 3 printf都显示了对size_t和%t对ptrdiff_t (C99)的%z的支持,但是这些都没有提到off_t。在野外的建议通常会提供对off_t的%u转换,在我看来,这是“足够正确的”(在64位和32位系统之间,unsigned int和off_t都是相同的)。

#7


-2  

I saw this post at least twice, because the accepted answer is hard to remeber for me(I rarely use z or j flags and they are seems not platform independant).

我至少两次看到这个帖子,因为接受的答案很难记住(我很少使用z或j标志,而且它们看起来不是平*立的)。

The standard never says clearly the exact data length of size_t, so I suggest you should first check the length size_t on your platform then select one of them:

标准从来没有明确表示size_t的确切数据长度,所以我建议您首先检查平台上的length size_t,然后选择其中一个:

if sizeof(size_t) == 4 use PRIu32
if sizeof(size_t) == 8 use PRIu64

And I suggest using stdint types instead of raw data types for consistancy.

我建议使用stdint类型而不是原始数据类型。

#8


-3  

As I recall, the only portable way to do it, is to cast the result to "unsigned long int" and use %lu.

我记得,唯一可移植的方法是将结果转换为“unsigned long int”并使用%lu。

printf("sizeof(int) = %lu", (unsigned long) sizeof(int));

#9


-5  

Please use %lu. Off_t is unsigned long.

请使用%。Off_t无符号长。

#10


-8  

use "%zo" for off_t. (octal) or "%zu" for decimal.

对off_t使用“%佐薇”。(八进制)或“%zu”为十进制。

#1


87  

You can use z for size_t and t for ptrdiff_t like in

你可以用z表示size_t,而t表示ptrdiff_t。

printf("%zu %zd", size, ptrdiff);

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:

但我的手册上说,一些旧图书馆使用的字符与z不同,并且不鼓励使用它。然而,它是标准化的(C99标准)。对于那些intmax_t和int8_t的stdint。h等等,有一些宏你可以用,像另一个答案说:

printf("value: %" PRId32, some_int32_t);
printf("value: %" PRIu16, some_uint16_t);

They are listed in the manpage of inttypes.h.

它们列在inttypes.h的manpage上。

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.

就我个人而言,我只需要将这些值转换成像另一个答案所建议的那样长或长。如果您使用C99,那么您可以(当然也应该)将其转换为unsigned long long或long long,并分别使用%llu或%lld格式。

#2


85  

To print off_t:

打印off_t:

printf("%jd\n", (intmax_t)x);

To print size_t:

打印size_t:

printf("%zu\n", x);

To print ssize_t:

打印ssize_t:

printf("%zd\n", x);

See 7.19.6.1/7 in the C99 standard, or the more convenient POSIX documentation of formatting codes:

在C99标准中见7.19.6.1/7,或更方便的格式代码POSIX文档:

http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html

http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html

If your implementation doesn't support those formatting codes (for example because you're on C89), then you have a bit of a problem since AFAIK there aren't integer types in C89 that have formatting codes and are guaranteed to be as big as these types. So you need to do something implementation-specific.

如果您的实现不支持那些格式代码(例如,因为您在C89上),那么您就有一点问题,因为在C89中没有整数类型,它有格式化代码,并且保证会像这些类型一样大。因此,您需要执行特定于实现的操作。

For example if your compiler has long long and your standard library supports %lld, you can confidently expect that will serve in place of intmax_t. But if it doesn't, you'll have to fall back to long, which would fail on some other implementations because it's too small.

例如,如果您的编译器有很长的时间,并且您的标准库支持%lld,那么您可以很自信地期望它可以代替intmax_t。但是如果不这样做,您将不得不退回到long,这将在其他一些实现上失败,因为它太小了。

#3


5  

For Microsoft, the answer is different. VS2013 is largely C99 compliant but "[t]he hh, j, z, and t length prefixes are not supported." For size_t "that is, unsigned __int32 on 32-bit platforms, unsigned __int64 on 64-bit platforms" use prefix I (capital eye) with type specifier o, u, x, or X. See VS2013 Size specification

对于微软来说,答案是不同的。VS2013基本上是C99兼容的,但“他hh、j、z和t长度前缀不受支持”。对于size_t“也就是说,在32位平台上没有签名的__int32,在64位平台上没有签名的__int64”使用前缀I (capital eye)和类型说明符o、u、x或x。

As for off_t, it is defined as long in VC\include\sys\types.h.

至于off_t,它在VC中定义的很长,包括\sys\types.h。

#4


3  

Which version of C are you using?

你用的是哪个版本的C ?

In C90, the standard practice is to cast to signed or unsigned long, as appropriate, and print accordingly. I've seen %z for size_t, but Harbison and Steele don't mention it under printf(), and in any case that wouldn't help you with ptrdiff_t or whatever.

在C90中,标准的做法是将签名或未签名的long类型转换为合适的,并相应地打印。我已经看到了size_t的%z,但是Harbison和Steele在printf()中没有提到它,在任何情况下都不能帮助您使用ptrdiff_t或其他任何东西。

In C99, the various _t types come with their own printf macros, so something like "Size is " FOO " bytes." I don't know details, but that's part of a fairly large numeric format include file.

在C99中,各种_t类型都有自己的printf宏,所以像“Size”这样的东西是FOO“字节”。我不知道细节,但这是一个相当大的数字格式的一部分,包括文件。

#5


3  

You'll want to use the formatting macros from inttypes.h.

您需要使用inttypes.h中的格式宏。

See this question: Cross platform format string for variables of type size_t?

请参见这个问题:类型size_t的变量的跨平台格式字符串?

#6


1  

Looking at man 3 printf on Linux, OS X, and OpenBSD all show support for %z for size_t and %t for ptrdiff_t (for C99), but none of those mention off_t. Suggestions in the wild usually offer up the %u conversion for off_t, which is "correct enough" as far as I can tell (both unsigned int and off_t vary identically between 64-bit and 32-bit systems).

在Linux、OS X和OpenBSD上查看man 3 printf都显示了对size_t和%t对ptrdiff_t (C99)的%z的支持,但是这些都没有提到off_t。在野外的建议通常会提供对off_t的%u转换,在我看来,这是“足够正确的”(在64位和32位系统之间,unsigned int和off_t都是相同的)。

#7


-2  

I saw this post at least twice, because the accepted answer is hard to remeber for me(I rarely use z or j flags and they are seems not platform independant).

我至少两次看到这个帖子,因为接受的答案很难记住(我很少使用z或j标志,而且它们看起来不是平*立的)。

The standard never says clearly the exact data length of size_t, so I suggest you should first check the length size_t on your platform then select one of them:

标准从来没有明确表示size_t的确切数据长度,所以我建议您首先检查平台上的length size_t,然后选择其中一个:

if sizeof(size_t) == 4 use PRIu32
if sizeof(size_t) == 8 use PRIu64

And I suggest using stdint types instead of raw data types for consistancy.

我建议使用stdint类型而不是原始数据类型。

#8


-3  

As I recall, the only portable way to do it, is to cast the result to "unsigned long int" and use %lu.

我记得,唯一可移植的方法是将结果转换为“unsigned long int”并使用%lu。

printf("sizeof(int) = %lu", (unsigned long) sizeof(int));

#9


-5  

Please use %lu. Off_t is unsigned long.

请使用%。Off_t无符号长。

#10


-8  

use "%zo" for off_t. (octal) or "%zu" for decimal.

对off_t使用“%佐薇”。(八进制)或“%zu”为十进制。