Skip to content
Related Articles

Related Articles

Improve Article

Maximum value of unsigned int in C++

  • Last Updated : 18 Jan, 2021

In this article, we will discuss the maximum value of unsigned int in C++.

  • Unsigned int data type in C++ is used to store 32-bit integers.
  • The keyword unsigned is a data type specifier, which only represents non-negative integers i.e. positive numbers and zero. 

Some properties of the unsigned int data type are:

  • An unsigned data type can only store positive values.
  • It takes a size of 32 bits.
  • A maximum integer value that can be stored in an unsigned int data type is typically 4, 294, 967, 295, around 232 – 1(but is compiler dependent).
  • The maximum value that can be stored in unsigned int is stored as a constant in the <climits> header file. whose value can be used as UINT_MAX.
  • A minimum integer value that can be stored in an unsigned int data type is typically 0.
  • In case of overflow or underflow of data type, the value is wrapped around.
  • For example, if 0 is stored in an int data type and 1 is subtracted from it, the value in that variable will become equal to 4, 294, 967, 295. Similarly, in the case of overflow, the value will round back to 0.

Below is the program to get the highest value that can be stored in unsigned int in C++:

C++




// C++ program to obtain the maximum
// value stored in unsigned int
#include <climits>
#include <iostream>
using namespace std;
  
// Function that prints the maximum
// value stored in unsigned int
void maxUnsignedInt()
{
    // From the constant of climits
    // header file
    unsigned int valueFromLimits = UINT_MAX;
  
    cout << "Value from climits constant : "
         << valueFromLimits << "\n";
  
    // Using the wrap around property
    // of data types
  
    // Initialize a variable with 0
    unsigned int value = 0;
  
    // Subtract 1 from value since
    // unsigned data type cannot store
    // negative number, the value will
    // wrap around and store the
    // maximum value in it
    value = value - 1;
  
    cout << "Value using the wrap"
         << " around property : "
         << value << "\n";
}
  
// Driver Code
int main()
{
    // Function call
    maxUnsignedInt();
  
    return 0;
}
Output:
Value from climits constant : 4294967295
Value using the wrap around property : 4294967295
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :