Tutorial

Using INT_MAX and INT_MIN in C/C++

Published on August 3, 2022
author

Vijaykrishna Ram

Using INT_MAX and INT_MIN in C/C++

In this article, we’ll take a look at using INT_MAX and INT_MIN in C/C++.

These are actually useful macros which represent the maximum and minimum integer values.

Let’s take a look at it, using some examples.


Using INT_MAX and INT_MIN

INT_MAX is a macro which represents the maximum integer value. Similarly, INT_MIN represents the minimum integer value.

These macros are defined in the header file <limits.h>, so you must include it.

#include <limits.h>

INT_MAX

INT_MIN

Note that any integer variable must lie between INT_MIN and INT_MAX.

Typically, integers are stored as 4 bytes (32 bits).

This means that in almost all machines, the maximum integer value will be 2^(31) - 1 = +2147483647.

The minimum integer value will be -(2^31) = -2147483648

Let’s verify this, for our machine.

#include <stdio.h>
#include <limits.h>

int main() {
    printf("Maximum Integer Value: %d\n", INT_MAX);
    printf("Minimum Integer Value: %d\n", INT_MIN);
    return 0;
}

Output

Maximum Integer Value: 2147483647
Minimum Integer Value: -2147483648

Indeed, we get what we predict.

Let’s now take another example, to correctly predict any integer overflow or underflow.

#include <stdio.h>
#include <limits.h>

int main() {
    int value = 0;
    while (value >= 0) {
        // Check for overflow
        if (value == INT_MAX) {
            printf("value = %d. Possible overflow!\n", value);
        }
        value ++;
    }
    printf("Now, value = %d\n", value);
    value = 0;
    while (value <= 0) {
        // Check for underflow
        if (value == INT_MIN) {
            printf("value = %d. Possible underflow!\n", value);
        }
        value --;
    }
    printf("Now, value = %d\n", value);
    return 0;
}

Output

value = 2147483647. Possible overflow!
Now, value = -2147483648
value = -2147483648. Possible underflow!
Now, value = 2147483647

While this takes a good few seconds to run, this does indeed do what we expect.

The integer will overflow to INT_MIN, and will underflow to INT_MAX.

This is useful to detect such jumps in the values.


Why do we need these macros?

Often, for certain algorithms, it is sometimes necessary to initialize a variable as the lowest/highest value.

The number of bits of the datatype may differ based on the machine.

To make the usage of the maximum/minimum values be consistent, it would be convenient if everyone could use the same macros!

This is exactly why these kinds of macros exist -

  • To spare you from remember the actual values
  • Have consistent programming patterns across all machines
  • Very convenient to use

Hopefully, these reasons may convince you to use such kinds of macros whenever you build your own C/C++ library.


Conclusion

In this article, we learned about using the INT_MAX and INT_MIN macros in C / C++.

For similar content, do go through our tutorial section on C programming.

References


Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Vijaykrishna Ram

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.