Handle Win64 printf or wsprintf Warnings

Submit New Article

November 24, 2008 11:00 PM PST



Challenge

Address Win64* compilation warnings related to improper use of Win64 printf or wsprintf format specifiers. Using improper format specifiers in printf or wsprintf will generate warnings. The warnings might look something like this:

C4313: Calling the printf family of functions with 
conflicting< conversion type specifiers and arguments

 

During the course of porting applications to the Win64 environment, it is necessary to identify and resolve compiler warnings and errors. The procedure for identifying these errors is addressed in the separate item "How to Use Makefiles to Resolve Win64 Porting Issues."


Solution

Use the proper format specifiers to fix this warning. For example, the following code generates a warning because it expects %p to be a 32-bit field:

printf("%p", int_value)
// this code expects %p to be 32 bits and will fail on 64-bit Intel(R) architecture

 

It could therefore be modified as follows:

printf("%x",int_value)
// change to %x for 64-bit Intel architecture to get the desired result

 

Likewise, in the case of the following code, %x is a 32 bit field and will truncate the pointer:

printf("%x", ptr_value)
// %x is a 32 bit field and will truncate the pointer

 

It could therefore be modified as follows:

printf("%p",ptr_value)
// change to %p to display the full pointer value

 

Win64 uses the LLP64 (or P64) uniform data model, in which pointers and long long are 64 bits, but int and long are 32 bits. To support this data model and to provide compatibility with the existing Win32 code, new data types have been defined in Win64.


Source

Porting Microsoft Windows* Applications to Intel® Itanium® Processor Systems