INT_MAX and INT_MIN Macro Expressions in C++

Jinku Hu Oct 12, 2023
  1. Use INT_MIN and INT_MAX to Access Type Specific Limits in C++
  2. Use INT_MIN and INT_MAX to Generate Random Numbers in C++
INT_MAX and INT_MIN Macro Expressions in C++

This article will demonstrate multiple methods about how to utilize INT_MAX and INT_MIN macro expressions in C++.

Use INT_MIN and INT_MAX to Access Type Specific Limits in C++

C++ language defines multiple built-in data types with some specifications about how much memory they should occupy and the corresponding maximum/minimum values. Data types like integers are commonly used in calculations where their possible maximum and minimum values need to be considered for the problem. Although the limits are dependent on the specific types’ storage size, these vary based on the hardware platform. Thus, we need to access these values with a fixed handle, hence the macro expressions - INT_MIN and INT_MAX. These correspond to the minimum and maximum values of the signed int data type. The following example demonstrates multiple macro expressions that are available under the <climits> header.

#include <climits>
#include <iostream>

int main() {
  printf("CHAR_BIT   = %d\n", CHAR_BIT);
  printf("MB_LEN_MAX = %d\n", MB_LEN_MAX);
  printf("\n");

  printf("CHAR_MIN   = %+d\n", CHAR_MIN);
  printf("CHAR_MAX   = %+d\n", CHAR_MAX);
  printf("SCHAR_MIN  = %+d\n", SCHAR_MIN);
  printf("SCHAR_MAX  = %+d\n", SCHAR_MAX);
  printf("UCHAR_MAX  = %u\n", UCHAR_MAX);
  printf("\n");

  printf("SHRT_MIN   = %+d\n", SHRT_MIN);
  printf("SHRT_MAX   = %+d\n", SHRT_MAX);
  printf("USHRT_MAX  = %u\n", USHRT_MAX);
  printf("\n");

  printf("INT_MIN    = %+d\n", INT_MIN);
  printf("INT_MAX    = %+d\n", INT_MAX);
  printf("UINT_MAX   = %u\n", UINT_MAX);
  printf("\n");

  printf("LONG_MIN   = %+ld\n", LONG_MIN);
  printf("LONG_MAX   = %+ld\n", LONG_MAX);
  printf("ULONG_MAX  = %lu\n", ULONG_MAX);
  printf("\n");

  printf("LLONG_MIN  = %+lld\n", LLONG_MIN);
  printf("LLONG_MAX  = %+lld\n", LLONG_MAX);
  printf("ULLONG_MAX = %llu\n", ULLONG_MAX);
  printf("\n");

  return EXIT_SUCCESS;
}

Output:

CHAR_BIT   = 8
MB_LEN_MAX = 16

CHAR_MIN   = -128
CHAR_MAX   = +127
SCHAR_MIN  = -128
SCHAR_MAX  = +127
UCHAR_MAX  = 255

SHRT_MIN   = -32768
SHRT_MAX   = +32767
USHRT_MAX  = 65535

INT_MIN    = -2147483648
INT_MAX    = +2147483647
UINT_MAX   = 4294967295

LONG_MIN   = -9223372036854775808
LONG_MAX   = +9223372036854775807
ULONG_MAX  = 18446744073709551615

LLONG_MIN  = -9223372036854775808
LLONG_MAX  = +9223372036854775807
ULLONG_MAX = 18446744073709551615

Use INT_MIN and INT_MAX to Generate Random Numbers in C++

Random numbers are often generated between some fixed range. It’s useful to have number data type limits to specify the range parameters accordingly and not overflow the generated results. The following example code utilizes the uniform_int_distribution to limit the range of the generated numbers. In this case, we specified the INT_MAX and INT_MIN values to retrieve the integers from the maximum available range.

#include <climits>
#include <iomanip>
#include <iostream>
#include <random>

using std::cout;
using std::endl;
using std::setprecision;

int main() {
  std::random_device rd;
  std::default_random_engine eng(rd());
  std::uniform_int_distribution<int> distr(INT_MIN, INT_MAX);

  for (int n = 0; n < 6; ++n) {
    cout << distr(eng) << "\n";
  }
  cout << endl;

  return EXIT_SUCCESS;
}
-895078088
1662821310
-636757160
1830410618
1031005518
-438660615
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Integer