pistach pistach - 1 year ago 173
C Question

Format specifier 32 bit - 64 bit

I'm using Visual Studio 2015 to build a project on 32bit platform and on 64 bit platform.

Sizeof returns an unsigned int in the 32 bit platform and an unsigned __int64 in the 64 bit platform.

I have the following code :

printf ("limit is %u. \n",sizeof(var));

I need to compile the same code on both platforms. Using the format specifier %u gives a warning in 64bit, using %I64u gives a warning in 32bit. Is there a way to use a formatspecifier on both platforms without writing the printf 2 times (with ifdef _WIN32). I have a lot of these warnings in my project. This would be a lot of work and wouldn't look 'clean'.

Answer Source

The type of sizeof is a size_t.

Use %zu as the format specifier for that type. It's guaranteed to be well-defined for all platforms.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download