Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

unsigned long — unsigned integer type

unsigned long is a standard unsigned integer type with a width of at least 32 bits. It is the same type as unsigned long int. Its exact range depends on the implementation, and arithmetic performed in this type follows modular rules.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

unsigned long name = 0UL;
// Alternative spelling (use instead of the declaration above):
// unsigned long int name = 0UL;

unsigned long is a built-in unsigned integer type corresponding to, but distinct from, long. The spellings unsigned long, unsigned long int, and long unsigned int denote the same type. Declaring it requires no header. The literal 42UL has type unsigned long; the UL suffix selects this type if the value fits, but can select unsigned long long otherwise.

Its width N is at least 32 bits, and its range is 0 through 2^N - 1, reaching at least 4294967295. The exact width is implementation-defined: do not assume that the type is always 32 or always 64 bits. std::numeric_limits<unsigned long>::max() from <limits> provides its largest value.

Arithmetic performed in unsigned long operates modulo 2^N: adding 1UL to the largest value produces 0. This behavior is defined, but it can still indicate a calculation bug—being unsigned does not protect against exceeding an intended range. The example demonstrates this without making its output depend on the type's width.

This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 edition; the rules described here also apply to C++23.

Examples

#include <iostream>
#include <limits>

int main() {
    unsigned long count = 42UL;
    unsigned long wrapped = std::numeric_limits<unsigned long>::max();
    wrapped += 1UL;

    std::cout << count << '\n' << wrapped << '\n';
}

Result

42
0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources