`unsigned` — C++ Keyword

`unsigned` — C++ Keyword

The unsigned keyword in C++: marks an integer type as unsigned (non-negative only).

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

unsigned

Marks an integer type as unsigned, meaning it holds only non-negative values but has twice the positive range of the corresponding signed type. Unsigned arithmetic wraps modulo $2^N$.

Syntax

unsigned char   uc;
unsigned short  us;
unsigned int    ui;   // 'int' optional: unsigned == unsigned int
unsigned long   ul;
unsigned long long ull;

Example

#include <print>
#include <climits>

int main() {
    unsigned int u = 4'294'967'295u;   // UINT_MAX on 32-bit int
    std::println("{}", u);              // 4294967295

    // Unsigned wrap-around is well-defined
    unsigned char uc = 255;
    ++uc;
    std::println("{}", uc);   // 0  (wraps)

    // Comparing signed and unsigned – common pitfall
    int n = -1;
    // if (n < u) ...  // n is converted to unsigned, becomes UINT_MAX
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}