简体   繁体   English

Perl API 类型的 printf 格式说明符

[英]printf format specifiers for Perl API types

Are there macros that provide the correct printf format specifiers for IV , UV , STRLEN , Size_t and SSize_t ?是否有宏为IVUVSTRLENSize_tSSize_t提供正确的printf格式说明SSize_t None are listed in perlapi . perlapi 中没有列出。


C provides macros for the format specifiers for the types provided by stdint.h , such as uint32_t . C 为stdint.h提供的类型的格式说明符提供宏,例如uint32_t

#include <inttypes.h>
#include <stdint.h>

uint32_t i = ...;
printf("i = %" PRIu32 "\n", i);

Is there something similar to PRIu32 for IV , UV , STRLEN , Size_t and SSize_t ?对于IVUVSTRLENSize_tSSize_t是否有类似于PRIu32东西?


The larger problem is that I'm trying to suggest a fix for the following compilation warnings produced when installing Sort::Key on Ubuntu on Windows Subsystem for Linux:更大的问题是,我试图建议修复在 Windows 子系统 Linux 上的 Ubuntu 上安装 Sort::Key 时产生的以下编译警告:

Key.xs: In function ‘_keysort’:
Key.xs:237:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘IV {aka long int}’ [-Wformat=]
      croak("unsupported sort type %d", type);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~
Key.xs: In function ‘_multikeysort’:
Key.xs:547:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘STRLEN {aka long unsigned int}’ [-Wformat=]
   croak("wrong number of results returned "
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Key.xs:547:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘IV {aka long int}’ [-Wformat=]
  • For UV , the following macros exist:对于UV ,存在以下宏:

    • UVuf (decimal) UVuf (十进制)
    • UVof (octal) UVof (八进制)
    • UVxf (lc hex) UVxf (lc 十六进制)
    • UVXf (uc hex) UVXf (uc 十六进制)
  • For IV , the following macro exists:对于IV ,存在以下宏:

    • IVdf (decimal) IVdf (十进制)
  • For NV , the following macros exist:对于NV ,存在以下宏:

    • NVef (" %e -ish") NVef (" %e -ish")
    • NVff (" %f -ish") NVff (" %f -ish")
    • NVgf (" %g -ish") NVgf (“ %g -ish”)
  • For Size_t and STRLEN , use the builtin z length modifier.对于Size_tSTRLEN ,使用内置的z长度修饰符。 [1] [1]

    • %zu (decimal) %zu (十进制)
    • %zo (octal) %zo (八进制)
    • %zx (lc hex) %zx (lc 十六进制)
    • %zX (uc hex) %zX (uc 十六进制)
  • For SSize_t , use the builtin z length modifier.对于SSize_t ,使用内置的z长度修饰符。 [1] [1]

    • %zd (decimal) %zd (十进制)

For example,例如,

IV iv      = ...;
STRLEN len = ...;

croak("iv=%" IVdf " len=%zu", iv, len);

  1. While Size_t and SSize_t are configurable, they're never different from size_t and ssize_t in practice, and STRLEN is a typedef for Size_t .虽然Size_tSSize_t是可配置的,但它们在实践中与size_tssize_t没有区别,并且STRLENSize_t

If Size_t is the same as size_t , then %zu is correct.如果Size_tsize_t相同,则%zu是正确的。

STRLEN is likely, but not certain, to be the same as size_t . STRLEN可能(但不确定)与size_t相同。

If SSize_t is the same as ssize_t , then %zd is probably correct (it's complicated).如果SSize_tssize_t相同,那么%zd可能是正确的(它很复杂)。

For other types, if you don't know what predefined type they correspond to, convert to a known type.对于其他类型,如果您不知道它们对应的预定义类型,请转换为已知类型。 Knowing the signedness helps.了解签名会有所帮助。 For example:例如:

some_unknown_signed_integer_type n = 42;
some_unknown_unsigned_integer_type x = 128;
printf("n = %jd\n", (intmax_t)n);
printf("x = %ju\n", (uintmax_t)x);

intmax_t and uintmax_t are defined in <stdint.h> . intmax_tuintmax_t<stdint.h>中定义。

You can get away with converting to long or unsigned long and using %ld or %lu , for example, if you happen to know that the type is no wider than long or unsigned long .例如,如果您碰巧知道该类型的宽度不超过longunsigned long ,则%lu转换为longunsigned long并使用%ld%lu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM