Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::int16_t — an exactly 16-bit signed integer type

std::int16_t from <cstdint> is an optional alias for a signed integer type with exactly 16 bits and no padding bits. It stores values from -32768 to 32767; whether it is available depends on the implementation.

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

Syntax

#include <cstdint>
std::int16_t value{123};

std::int16_t, provided by <cstdint>, is an alias for an existing signed integer type, not a new, distinct type. It has exactly 16 bits, no padding bits, and a range from -32768 to 32767. Do not assume it always aliases short: short is guaranteed at least 16 bits, not exactly 16.

This alias is optional. An implementation must provide it if it supports a corresponding exact-width type without padding bits. After including <cstdint>, you can check availability with #ifdef INT16_MAX. When at least 16 bits is sufficient, consider std::int_least16_t instead of requiring an exact width.

A variable's width does not automatically determine the width of calculations: integral promotions can convert an operand to int. Signed arithmetic overflow has undefined behavior; do not rely on results wrapping around. The example explicitly converts the value to int and performs an addition whose result fits in that type.

This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 publication. A repository compiler flag does not attest to the production runtime environment.

Examples

#include <cstdint>
#include <iostream>

#ifndef INT16_MAX
#error "This example requires std::int16_t"
#else
int main() {
    std::int16_t value{1200};
    const int result = static_cast<int>(value) + 300;
    std::cout << result << '\n';
}
#endif

Result

1500

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