27

I want to recommend the use of <inttypes.h> to someone doing printf with mixed 32/64 bit builds. I tried to Google an introduction or tutorial page with a few examples and usage guidelines, but I couldn't find one.

Can someone recommend an introduction or tutorial for <inttypes.h>?

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • What exactly do you mean by "mixed 32/64 bit builds". Will the user be expected to use the 'fixed size' types from `stdint.h`? Is there something in particular lacking in Edwin's answer that led to the bounty being added? – Michael Burr Jun 15 '11 at 00:44
  • 1
    @Michael: Yes, the issue arises when you use known-width integer types like `int64_t` under LP64 where that corresponds to `long`. The proper format specifier is `%ld` in that case, but if you build on a 32-bit system where it corresponds to `long long` then you need `%lld`. I was hoping for a description which both motivated the use of `` (as I briefly do in this comment) as well as providing examples. Something I could point at to save myself having to explain the why and the how. – Ben Jackson Jun 15 '11 at 04:02

4 Answers4

44

Try http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/inttypes.h.html for a start.

A better example of how to use the new portable formatting macros was found in avr-libc. I've included an example (from the link) to illustrate. QNX libraries also have a better human-readable description (if you don't like reading the specification cold), although you have to scroll nearly to the end of the page to get to the meat of the descriptions.

#include <inttypes.h>

uint8_t smallval;
int32_t longval;
...
printf("The hexadecimal value of smallval is %" PRIx8
       ", the decimal value of longval is %" PRId32 ".\n",
       smallval, longval);

Note that this uses the "String" "String" implied concatenation operator to yield the string (in this example)

"The hexadecimal value of smallval is %x, the decimal value of longval is %ld.\n"

An attempt to decompose the naming convention seems to indicate:

  • (first three letters)
    • PRI for printf format
    • SCN for scanf format
  • (fourth letter)
    • x for hexadecimal formatting
    • u for unsigned formatting
    • o for octal formatting
    • i for integer formatting
    • d for decimal formatting
  • (extra letters)
    • 8 for eight bit
    • 16 for sixteen bit
    • 32 for thirty-two bit
    • 64 for sixty-four bit
    • FAST8 for "fast" eight bit
    • FAST16 for "fast" sixteen bit
    • FAST32 for "fast" thirty-two bit
    • FAST64 for "fast" sixty-four bit
    • LEAST8 for "least" eight bit
    • LEAST16 for "least" sixteen bit
    • LEAST32 for "least" thirty-two bit
    • LEAST64 for "least" sixty-four bit
    • PTR for pointer
    • MAX for maximum supported bit size

so PRIx8 means printf format instruction to format to hexadecimal eight bits.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Ah, what a good use of putting string literals beside each other. – Seth Carnegie May 09 '12 at 00:28
  • 2
    PTR is for pointer-sized _integers_ (size_t,intptr_t,ptrdiff_t), not pointers. On at least one of my platforms (OSX), I get an error using PRIxPTR with void*. Use %p for pointers. – Rick Berge Jul 08 '13 at 19:55
  • @RickBerge: Note that strictly the PTR macros are for `intptr_t` and `uintptr_t`; the C99 and C11 standards define the format modifier `z` to designate a `size_t` argument (hence `"%zu"`, for example), and they define `t` to designate a `ptrdiff_t` (hence `"%td"`, for example). – Jonathan Leffler Mar 31 '15 at 14:45
8

I always go to the standard (PDF link) for those things; they're not too complicated once you figure out the patterns they're set up in. The relevant section is §7.8 Format conversion of integer types <inttypes.h>.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
2

HP Has a good reference on writing portable code and they give some specific advice for using inttypes.h

Writing Portable Code

ccozad
  • 1,119
  • 8
  • 13
1

I always start at Wikipedia to look up a header. <inttypes.h> seems to be very problematic on Wikipedia. The next step I would then take is going to this site. This previous site lists every macro and does give an example. You could also check out this site, which actually shows you the header file. I don't think that any of those are really tutorials per say but they are a good jumping off point.

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57