Two issues to note about printf: unsigned integers and 64-bit integers

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.