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>
?
The purpose of <inttypes. h> is to provide a set of integer types whose definitions are consistent across machines and independent of operating systems and other implementation idiosyncrasies. It defines, via typedef, integer types of various sizes.
The inttypes. h file is a C header file that is part of the C standard library and API. It was added with the 1999 version of the ISO C standard (known as C99). It includes the stdint.
so PRIx8 means printf format instruction to format to hexadecimal eight bits.
PRIu64 is a format specifier, introduced in C99, for printing uint64_t , where uint64_t is (from linked reference page): unsigned integer type with width of ... 64 bits respectively (provided only if the implementation directly supports the type)
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:
so PRIx8
means printf format instruction to format to hexadecimal eight bits.
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With