The following example uses the Visual Studio.NET 32-bit C + + compiler.
1) The difference between%d and%u
unsigned int dwvalue;
printf ("%d", dwvalue);
When the value of the dwvalue is greater than 0x7fffffff, the result of the output becomes negative.
The correct program should be:
printf ("%u", dwvalue);
2) using 64-bit integers
Longlong Llvalue;
int ivalue;
printf ("%d,%d", Llvalue, Ivalue);
Ivalue value will never be output, the first%d output is llvalue low 32 bits, the second%d output is llvalue high 32 bits. So the program should be modified to:
printf ("%i64d,%d", Llvalue, Ivalue);
These two examples show that the type of printf () is not secure. If the data type of the parameter for printf () is modified and the format string is not modified, such a hidden error is sometimes difficult to find, so when you change the data type, be aware that you need to modify the corresponding format string, or the compiler will not even have a warning message.