Maximum value of unsigned int in C++
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++:
valueFromLimits = UINT_MAX;
"Value from climits constant : "
<< valueFromLimits <<
value = 0;
value = value - 1;
"Value using the wrap"
" around property : "
<< value <<
Value from climits constant : 4294967295
Value using the wrap around property : 4294967295
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!