<climits> library in C++

FREE BOOK -> Problems for the day before your Coding Interview (on Amazon)

Price is set to 0. Click on Buy Now and it will be downloaded in your account.
Internship at OpenGenus

In this article, we will learn about climits library in C++. This library is used for managing and evaluating maximum and minimum values of several integer data-types.

  • The climits is also known as limits.h library in C++.
  • Sometimes it is very difficult for us to remember every limiting value of every type of data-type in C++. Hence we require a library which automatically tells us the limiting values of the datatypes.
  • So instead of remembering all such values we can directly use such macros which makes the programming far easier.
  • climits or limits.h defines sizes of integral types.
  • NOTE: The actual value is system and library dependent, but the results gets reflected in the target system.
  • Your Machine’s values depends upon whether it is 32 bit machine or 64 bit machine.

Macro Constants

The complete list of macro constants are:

  • CHAR_MIN : -128
  • CHAR_MAX : 127
  • 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

1. CHAR_MIN :

CHAR_MIN: Minimum value for an object of type char (-127 or 0)

Minimum value for an object of type char
Value of CHAR_MIN is either -127 or less* or 0

2. CHAR_MAX :

CHAR_MAX: Maximum value for an object of type char (127 or 255)

Maximum value for an object of type char
Value of CHAR_MAX is either 127 or 255  or greater*

3. SHRT_MAX :

SHRT_MAX: Maximum value for an object of type short int (32767)

Maximum value for an object of type short int
Value of SHRT_MAX is 32767 or greater*

4. SHRT_MIN :

SHRT_MIN: Minimum value for an object of type short int (-32767)

Minimum value for an object of type short int
Value of SHRT_MIN is -32767 or less*

5. USHRT_MAX :

Maximum value for an object of type unsigned short int    
Value of USHRT_MAX is 65535 or greater*

6. INT_MIN :

Minimum value for an object of type int    
Value of INT_MIN is -32767 or less*

7. INT_MAX :

Maximum value for an object of type int    
Value of INT_MAX is 32767 or greater*

8. UINT_MAX :

Maximum value for an object of type unsigned int    
Value of UINT_MAX is 65535 or greater*

9. LONG_MIN :

Minimum value for an object of type long int    
Value of LONG_MIN is -2147483647 or less*

10. LONG_MAX :

Maximum value for an object of type long int    
Value of LONG_MAX is 2147483647 or greater*

11. ULONG_MAX :

Maximum value for an object of type unsigned long int    
Value of ULONG_MAX is 4294967295 or greater*

12. LLONG_MIN :

Minimum value for an object of type long long int    
Value of LLONG_MIN is -9223372036854775807 or less*

13. LLONG_MAX :

Maximum value for an object of type long long int    
Value of LLONG_MAX is 9223372036854775807 or greater*

14. ULLONG_MAX :

Maximum value for an object of type unsigned long long int    
Value of ULLONG_MAX is 18446744073709551615 or greater*

Implementation

  • This program displays the respective values of macros on your machine :
#include <climits> 
#include <iostream>  
using namespace std;  
int main() 
{ 
    cout << "CHAR_MIN : " << CHAR_MIN << endl; 
    cout << "CHAR_MAX : " << CHAR_MAX << endl; 
    cout << "SHRT_MIN : " << SHRT_MIN << endl; 
    cout << "SHRT_MAX : " << SHRT_MAX << endl; 
    cout << "USHRT_MAX : " << USHRT_MAX << endl; 
    cout << "INT_MIN : " << INT_MIN << endl; 
    cout << "INT_MAX : " << INT_MAX << endl; 
    cout << "UINT_MAX : " << UINT_MAX << endl; 
    cout << "LONG_MIN : " << LONG_MIN << endl; 
    cout << "LONG_MAX : " << LONG_MAX << endl; 
    cout << "ULONG_MAX : " << ULONG_MAX << endl; 
    cout << "LLONG_MIN : " << LLONG_MIN << endl; 
    cout << "LLONG_MAX : " << LLONG_MAX << endl; 
    cout << "ULLONG_MAX : " << ULLONG_MAX << endl; 
    return 0; 
}

OUTPUT :

CHAR_MIN : -128
CHAR_MAX : 127
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

Applications

  1. These macros are used to check integer overflow
  2. They help a lot in computing minimum or maximum for an array by acting as a reference to check.

With this article at OpenGenus IQ, you must have got a complete idea of climits library in C++. Enjoy.