perlxs

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

Solution

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