4

Are there macros that provide the correct printf format specifiers for IV, UV, STRLEN, Size_t and SSize_t? None are listed in perlapi.


C provides macros for the format specifiers for the types provided by stdint.h, such as 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?


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:

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=]

2 Answers 2

6
  • For UV, the following macros exist:

    • UVuf (decimal)
    • UVof (octal)
    • UVxf (lc hex)
    • UVXf (uc hex)
  • For IV, the following macro exists:

    • IVdf (decimal)
  • For NV, the following macros exist:

    • NVef ("%e-ish")
    • NVff ("%f-ish")
    • NVgf ("%g-ish")
  • For Size_t and STRLEN, use the builtin z length modifier.[1]

    • %zu (decimal)
    • %zo (octal)
    • %zx (lc hex)
    • %zX (uc hex)
  • For SSize_t, use the builtin z length modifier.[1]

    • %zd (decimal)

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.
3

If Size_t is the same as size_t, then %zu is correct.

STRLEN is likely, but not certain, to be the same as size_t.

If SSize_t is the same as ssize_t, then %zd is probably correct (it's complicated).

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>.

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.

1
  • 2
    I was pretty sure Perl does provide appropriate macros, and I have located them. See my answer.
    – ikegami
    Feb 7, 2020 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.