c - printf and (signed) int of unknown size -
i have platform dependent type defined in code: typedef uint64_t mytype;
however on more limited platform, might 32 bits.
how printf it? in, in current situation, can use %llu, if on platform it's 32 bits, not best idea.
i thought using macros, know of better way? i'd love hear format specifier take length next argument, example.
since have platform-specific types, should easy enough use platform-specific format strings well, like:
#ifdef using_64_bits typedef uint64_t mytype; #define my_type_fmt priu64 #else typedef uint32_t mytype; #define my_type_fmt priu32 #endif then can use with:
mytype var1 = 42, var2 = 99; printf ("%6" my_type_fmt ", %019" my_type_fmt "\n", var1, var2); the extraction of % format string allows insert other format specifiers dynamically, such field widths , padding characters.
you'll notice i've avoided %llu-style format specifiers, should using more targeted ones in inttypes.h since implementation give correct 1 type.
Comments
Post a Comment