CKTIS / C++
unsigned int — unsigned integer type
unsigned int stores nonnegative integers and is a distinct type from int. It has at least 16 value bits, and its arithmetic operates modulo 2^N. Learn the equivalent unsigned spelling and the pitfalls of conversions and comparisons involving signed integers.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
unsigned int name{0u};
// Equivalent alternative: unsigned name{0u};unsigned int is a fundamental unsigned integer type, distinct from int. The spelling unsigned denotes exactly the same type. The declaration unsigned int count{3u}; creates a variable with value 3; 3u is a literal of type unsigned int.
For width N, the range is 0 through 2^N − 1. N is at least 16 and is implementation-defined: do not assume 32 bits. unsigned int has the same width and occupies the same amount of storage as int, but cannot represent negative values.
Addition, subtraction, and multiplication performed in unsigned int produce results modulo 2^N: adding 1u to the maximum value yields 0, and 0u - 1u yields the maximum value. This is defined wraparound, not the undefined behavior of signed arithmetic overflow. It does not replace range checks when wrapping would be a program error.
Converting an integer to unsigned int also works modulo 2^N, so static_cast<unsigned int>(-1) produces the maximum value. When comparing int with unsigned int, the int operand is converted to unsigned int; consequently, -1 < 1u is false. Do not choose this type merely to reject negative input: validate that input before conversion.
The example demonstrates initialization, wraparound, and the comparison pitfall without assuming a particular width. This entry targets C++23; the specified compilation mode does not attest a production environment. The eel.is source is an evolving draft, not a frozen ISO C++23 publication.
Examples
#include <iostream>
int main() {
unsigned int count{3u};
unsigned int maximum = static_cast<unsigned int>(-1);
unsigned int wrapped = maximum + 1u;
std::cout << count << '\n';
std::cout << wrapped << '\n';
std::cout << std::boolalpha << (-1 < 1u) << '\n';
}
Result
3 0 false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.