Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross platform format string for variables of type size_t? [duplicate]

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?

like image 965
twk Avatar asked Oct 06 '08 14:10

twk


1 Answers

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); 
like image 191
finnw Avatar answered Sep 19 '22 08:09

finnw