`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 (C)

Marks an integer type as unsigned: values range from 0 to $2^N - 1$. Unsigned arithmetic wraps modulo $2^N$ (well-defined).

Syntax

unsigned char   uc;
unsigned short  us;
unsigned int    u;    /* 'int' is optional */
unsigned long   ul;
unsigned long long ull;

Example

#include <stdio.h>
#include <limits.h>

int main(void) {
    unsigned int u = UINT_MAX;
    printf("%u\n", u);      /* 4294967295 (on 32-bit int) */

    /* Well-defined wrap */
    unsigned char uc = 255;
    ++uc;
    printf("%u\n", uc);     /* 0 */

    /* Sizes and counts should be unsigned */
    unsigned int len = 10;
    for (unsigned int i = 0; i < len; ++i) {
        /* use i ... */
    }
    return 0;
}

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;
}