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. Hello everyone, the last time we said is a clever use of the example 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.

Description: On a 32-bit computer, assume that 4 bytes are used to store variables of type int, which means that 32 bits of storage space are used.

At this time the value range of the INT type variable is: Negative 2 of 32 to positive 2 32 square-1, the detailed 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're knocking at the code.

Note that an overflow in the program can result in an error.

Just overflow has a feature: the maximum value plus one will be due to overflow

To the minimum value, and vice versa. The minimum value minus one will become the maximum value due to the overflow. We are able to skillfully exploit this feature of overflow . to ask

The maximum value of the various types of variables.


Crossing, they are. No code is written in the text. The specific 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.

Everyone can expand on their own. Calculates the maximum value for 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 number is 4.8.2. In this programming environment, a variable of type int is stored with 4 bytes. So the resulting results such as the following:

The min value of int is:-2147483648

The max value of int is:2147483647


Let's say that everyone's programming environment is different from mine, which may result in various calculations. Just one thing to be sure of. Assume that you use 4

Bytes store variables of type int, regardless of the environment. Are the same as the results above.


You crossing, the example of calculating the maximum value of a skillfully used overflow is here. I want to know what the following example, and listen to tell.


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.