A talk C Chestnut bar (34th time: C Language Example---------calculate maximum with overflow)

Source: Internet
Author: User
Tags linux mint


Ladies and gentlemen, crossing, the last time we talked about the example of the use of displacement, this time we say the example is: skillfully use overflow calculation of the maximum value.

Gossip Hugh, words return to the positive. Let's talk C chestnuts together!


As we all know, the variables in the program have a range of values, and this range is related to both the computer and the compiler. Let's give a practical example.

Note: On a 32-bit computer, if you use 4 bytes to store a variable of type int, that means 32 bits of storage space is used.

At this time, the value range of the INT type variable is: Negative 2 of the 32 square to positive 2 32 square-1, the specific value is 2147483648 to 2147483647.

If you assign a value that is not in this range to a variable of type int, an overflow occurs. That's what we need to do when we write the program.

Note that an overflow in the program can result in an error. However, the overflow has a feature: the maximum value plus one will be due to overflow

to the minimum value, and conversely, the minimum value minus one will cause the overflow to become the maximum value . We can skillfully use this feature of overflow to seek

The maximum value of the various types of variables.


Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use. In the code I

They calculate the maximum value of the INT type variable. You can expand yourself to calculate the maximum value of other types of variables.


Our current programming environment is this: the computer is a X86 structure, the operating system is a 32-bit Linux Mint system, the compiler is GCC,

The version is 4.8.2. In such a programming environment, a variable of type int is stored using 4 bytes, so the result is as follows:

The min value of int is:-2147483648

The max value of int is:2147483647


If everyone's programming environment is not the same as mine, it may get different results. But one thing is for sure, if you use 4

Bytes store variables of type int, and in whatever environment, the result is the same as above.


You crossing, the best example of using overflow to calculate the most value is here. I want to know what the following example, and listen to tell.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A talk C Chestnut bar (34th time: C Language Example---------calculate maximum with overflow)

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.