S
Stephan Beal
Hi, all!
i am working on a piece of software where the size of one of the core
int types is configurable to be 8, 16, 32, or 64 bits (uint8_t ..
uint64_t). That's all working fine and well, but i've got a slight
problem when it comes to outputing them: my printf() specifiers have
to be different depending on the exact width of the type. (Granted,
all of that is debugging code, and doesn't directly affect the
library, but it annoys the hell out of me nonetheless.)
e.g.:
typedef uint32_t id_type;
....
id_type x = 34;
printf("%u\n", x );
that'll work, it seems, as long as i'm on a 32-bit platform. If i
reconfigure the software to use:
typedef uint64_t id_type;
then i've got a problem: i've got to change the printf() to use to
%llu or %lu (depends on the bitness of the platform, apparently) to
avoid a warning from the compiler (a reasonable warning, IMO, and not
one i want to ignore).
Is there a canon solution to dealing with this?
A related point: what are the proper printf specifiers to use for each
uint type (8-64 bits)? As best as i can determine they are:
uint8_t: %hhu
uint16_t: %hu
uint32_t: %u
uint64_t: %llu or %lu?
:-?
i seem to remember reading that %hhX and %llX are GNU-specific (or not
standard), but i can't find anything to back that up with at the
moment.
Are there *standard* specifiers i can reliably use for each of those
types?
i am working on a piece of software where the size of one of the core
int types is configurable to be 8, 16, 32, or 64 bits (uint8_t ..
uint64_t). That's all working fine and well, but i've got a slight
problem when it comes to outputing them: my printf() specifiers have
to be different depending on the exact width of the type. (Granted,
all of that is debugging code, and doesn't directly affect the
library, but it annoys the hell out of me nonetheless.)
e.g.:
typedef uint32_t id_type;
....
id_type x = 34;
printf("%u\n", x );
that'll work, it seems, as long as i'm on a 32-bit platform. If i
reconfigure the software to use:
typedef uint64_t id_type;
then i've got a problem: i've got to change the printf() to use to
%llu or %lu (depends on the bitness of the platform, apparently) to
avoid a warning from the compiler (a reasonable warning, IMO, and not
one i want to ignore).
Is there a canon solution to dealing with this?
A related point: what are the proper printf specifiers to use for each
uint type (8-64 bits)? As best as i can determine they are:
uint8_t: %hhu
uint16_t: %hu
uint32_t: %u
uint64_t: %llu or %lu?
:-?
i seem to remember reading that %hhX and %llX are GNU-specific (or not
standard), but i can't find anything to back that up with at the
moment.
Are there *standard* specifiers i can reliably use for each of those
types?