CKTIS / C++
Unsigned and hexadecimal integer literal suffixes in C++
Distinguish a literal’s base from its type and from the type used in arithmetic. A `u` suffix alone neither guarantees 32 bits nor forces byte-width arithmetic.
Version: ISO C++23; implementation support may vary
Syntax
0x<hex-digits><optional-integer-suffix> or 0X<hex-digits><optional-integer-suffix>
Suffix components: u/U (unsigned), l/L (long), ll/LL (long long), z/Z (size, since C++23).
Use a single component or combine u/U with exactly one of l/L, ll/LL, or z/Z, in either order. lL and Ll are not valid long-long suffixes.
Examples: 0xFF, 0xFFu, 0xFFUL, 0xFFULL, 0xFFuz, 255u.When reading unsigned and hexadecimal integer literal suffixes, start with the prefix: 0x or 0X means base 16, not unsigned. The compiler chooses the first type in the list for the base and suffix that can represent the value. With no suffix, decimal tries int, long, long long; hexadecimal tries int, unsigned int, long, unsigned long, long long, unsigned long long. So 0xFF has type int.
If no listed type can represent the value and the literal has no size suffix (z or Z, with or without u or U), the compiler may use an extended integer type that can represent it. If the list contains only signed types or only unsigned types, the extended type must have the same signedness. If the list contains both, either signedness is allowed. If no permitted type can represent the value, the program is ill-formed.
With u or U, the order is unsigned int, unsigned long, unsigned long long. 0xFFu and 255u have the same value and type, unsigned int, which need not be 32 bits wide. UL starts at unsigned long and falls back to unsigned long long if needed. ULL starts at unsigned long long. The u component can come before or after the other component; ll and LL are valid, but lL and Ll are not.
Since C++23, uz gives std::size_t if the value fits. z alone tries the corresponding signed type first; only for a non-decimal literal is std::size_t the next candidate. Compiler support for the size suffix may vary. None of these suffixes promises an exact width; if you need exactly 32 bits, use std::uint32_t from <cstdint> where available.
A literal’s type is not necessarily the type of the arithmetic. For built-in +, integer operands ranked below int promote to int if it can represent their entire range, otherwise to unsigned int. std::uint8_t promotes to int; adding 1 to std::uint16_t can yield int or unsigned int. The usual arithmetic conversions determine the common type: byte + 1 yields int, while byte + 1u yields unsigned int.
The addition first produces 256. Only the conversion to std::uint8_t produces 0 modulo 256; this is not overflow in 8-bit arithmetic. When std::uint8_t aliases unsigned char, std::cout << stored selects the character overload. static_cast<unsigned int>(stored) selects numeric output.
Examples
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <type_traits>
static_assert(std::is_same_v<decltype(0xFFu), unsigned int>);
static_assert(std::is_same_v<decltype(0xFFUL), unsigned long>);
static_assert(std::is_same_v<decltype(0xFFULL), unsigned long long>);
static_assert(std::is_same_v<decltype(0xFFuz), std::size_t>);
int main() {
std::uint8_t byte{0xFFu};
std::uint16_t word{40'000};
static_assert(std::is_same_v<decltype(byte + 1), int>);
static_assert(std::is_same_v<decltype(byte + 1u), unsigned int>);
auto sum = byte + 1;
auto stored = static_cast<std::uint8_t>(sum);
std::cout << sum << ' ' << static_cast<unsigned int>(stored)
<< ' ' << (word + 1) << '\n';
}
Result
256 0 40001
The example requires C++23 mode with `uz` support and the types `std::uint8_t` and `std::uint16_t`. The expected line `256 0 40001` shows the sum, the converted value, and the result of adding to a 16-bit operand.
Related lessons
- C++ · Lesson 17 · Advanced · Version: 3
Sources
- N4950 — Working Draft, Standard for Programming Language C++: Integer literals [lex.icon]
- Integer literals [lex.icon]
- Integral promotions [conv.prom]
- Usual arithmetic conversions [expr.arith.conv]
- Integral conversions [conv.integral]
- Header <cstdint> synopsis [cstdint.syn]
- N4971 — Working Draft, Standard for Programming Language C++: Character inserter function templates [ostream.insertors.character]