C Programming – Handling printf with sizeof on 32 vs 64 Platforms

32bit-64bitc++

I have some code that prints the amount of memory used by the program. The line is similar to this:

printf("The about of RAM used is %u", anIntVariable*sizeof(double) );

where anIntVariable is an int variable for the number of elements of the double array. Anyhow, on 32-bit systems I never had any problems but on 64-bit systems, I get a compiler warning about using "%u" for a unsigned long integer. Using "%lu" as the format code fixes the problem on 64-bit but causes the compiler to complain on 32-bit because the type is back to unsigned int. I've found that, indeed, sizeof(double) returns a different value on 32 vs 64 bit systems. I've found some webpage guides to convert code from 32 bit to 64 bit But I'd rather have code that works on both instead of just converting back and forth.

How do I write this line in a platform independent way? I know many ways I could do it using preprocessor directives but that seems like a hack. Surely there's an elegant way that I'm not realizing.

Best Answer

Portable printf identifiers are provided in the include file inttypes.h or here.

This include file has many portable identifiers for your specific runtime. For your example, you want PRIuPTR, which means "PRintf Identifier unsigned with size of up to a pointer's size".

Your example will then be:

printf("The amount of RAM used is %" PRIuPTR, anIntVariable*sizeof(double) );

Results on 64bit Linux with GCC 4.3 (int anIntVariable = 1):

$ gcc test.c -m32 -o test && ./test
The amount of RAM used is 8
$ gcc test.c -o test && ./test
The amount of RAM used is 8

For completeness sake, there are identifiers for scanf too, whose prefixes are SCN.