Tutorials  /  Linux Basics

Using INT_MAX and INT_MIN in C/C++

Ccentron Redaktion · January 2024 ·3 min read ·Linux Basics, Tutorial

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 , so you must include it.

Code
#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.

Code
#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

Code
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.

Code
#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

Code
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.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

INT_MAX and INT_MIN: 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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren