Source-Changes archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Defining more PRIxxx macros



[redirecting from source-changes to tech-userlevel]

On Fri, 16 Jan 2009, Robert Elz wrote:
> I'd suggest that every _t type defined (anywhere in the basic system
> anyway) should have a PRI macro defined along with it (the sole
> exception being types that it is impossible to print with any simple
> format - structs and such).

I am very pleased to see support for this idea.

C99 defined a bunch of macros with names following the pattern

        "PRI"
        + printf format letter (one of "d"|"i"|"o"|"u"|"x"|"X")
        + uppercase abbreviation of type name (with trailing "_t"
          and leading "int" or "uint" removed)

I'd suggest continuing that pattern, and also removing a leading "s"
where it stands for a signed variant of a type.  So:

    already defined in <inttypes.h>:

        [u]int32_t -> PRId32, PRIi32, PRIo32, PRIu32, ...
        [u]intmax_t -> PRIdMAX, PRIiMAX, PRIoMAX, PRIuMAX, ...
        [u]intptr_t -> PRIdPTR, PRIiPTR, PRIoPTR, PRIuPTR, ...
        etc.

    proposed new macros to be defined in the same headers that define the
    coresponding types:

        off_t -> PRIdOFF, PRIiOFF, PRIoOFF, PRIuOFF, ...
        [s]size_t -> PRIdSIZE, PRIiSIZE, PRIoSIZE, PRIuSIZE, ...
        dev_t -> PRIdDEV, PRIiDEV, PRIoDEV, PRIuDEV, ...
        time_t -> PRIdTIME, PRIiTIME, PRIoTIME, PRIuTIME, ...
        etc.

> I've been meaning to suggest this as a policy for some time now -
> originally I was planning on exempting the [u]intNN_t types, but even
> those I believe should have PRI macros.

Those already have PRI macros, defined in <inttypes.h>.

> Only variables of type "int" should be printed %d only type long int
> %ld (etc) - anything declared with a _t manufactured type should be
> printed using a defined for the type PRI macro (ie: one associated
> with the specific type used, no sharing of PRI macros for more than
> one _t type, ever.)

I quite agree.  In the absence of the proposed new PRI macros, I have
been doing things like casting to intmax_t and printing with PRIdMAX or
%jd.

--apb (Alan Barrett)


Home | Main Index | Thread Index | Old Index