On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id. Is there an elegant way to handle this?
The PRIuPTR
macro (from <inttypes.h>) defines a decimal format for uintptr_t
, which should always be large enough that you can cast a size_t
to it without truncating, e.g.
fprintf(stream, "Your size_t var has value %" PRIuPTR ".", (uintptr_t) your_var);
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