Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::uint8_t — 8-bit unsigned integer type

std::uint8_t from <cstdint> is an optional alias for an unsigned integer type with exactly 8 bits and a range of 0–255. It undergoes integral promotions in calculations and may behave as a character when printed.

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

Syntax

#include <cstdint>
std::uint8_t value{42};

std::uint8_t, declared in <cstdint>, aliases an unsigned integer type with exactly 8 bits and no padding bits. It stores values from 0 to 255. The alias does not create a new, distinct type. It is useful for representing 8-bit numeric values, such as fields in a binary protocol.

This type is optional: an implementation provides it if it has a suitable integer type with the exact width and no padding bits. Do not assume it exists on every platform. When at least 8 bits are sufficient, consider std::uint_least8_t. The supplied standard URL points to an evolving working draft, not a frozen C++23 edition.

In value + 1, the std::uint8_t value is promoted to int, so when value is 255, the result is 256, not 0. Only converting that result to std::uint8_t produces 0: an integral conversion to this type reduces the value modulo 256. Do not confuse promotion during a calculation with a later conversion of its result.

std::uint8_t may alias unsigned char, so directly using std::cout << value may print a character rather than a number. Use static_cast<unsigned int>(value) to force numeric output. The example shows the initial value, the addition result, and the result of an explicit conversion back to the 8-bit type.

Examples

#include <cstdint>
#include <iostream>

int main() {
    std::uint8_t value{255};
    const auto sum = value + 1;
    const auto wrapped = static_cast<std::uint8_t>(sum);

    std::cout << static_cast<unsigned int>(value) << '\n';
    std::cout << sum << '\n';
    std::cout << static_cast<unsigned int>(wrapped) << '\n';
}

Result

255
256
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