Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Integer type limits and widths

Macros from <limits.h> describe an implementation's integer ranges and widths. Use them instead of assuming an 8-bit byte or a 32-bit int.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <limits.h>
/* Use object-like macros directly: INT_MIN, INT_MAX, INT_WIDTH. */

<limits.h> provides object-like macros. Minimum/maximum pairs are: SCHAR_MIN/SCHAR_MAX (signed char), SHRT_MIN/SHRT_MAX (short), INT_MIN/INT_MAX (int), LONG_MIN/LONG_MAX (long), and LLONG_MIN/LLONG_MAX (long long). Corresponding unsigned maxima are UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, ULLONG_MAX; their minimum is 0.

CHAR_MIN/CHAR_MAX depend on plain char signedness: they match the signed char limits or the range 0–UCHAR_MAX. CHAR_BIT counts bits per byte, at least 8. CHAR_WIDTH, SCHAR_WIDTH, UCHAR_WIDTH equal it. The pairs SHRT_WIDTH/USHRT_WIDTH, INT_WIDTH/UINT_WIDTH, LONG_WIDTH/ULONG_WIDTH, LLONG_WIDTH/ULLONG_WIDTH describe the corresponding types' widths and are equal within each pair.

Width includes the sign bit but excludes padding bits; it need not equal sizeof(type) * CHAR_BIT. C23 adds the _WIDTH macros and BITINT_MAXWIDTH. BOOL_WIDTH equals 1. C23 does not define BOOL_MAX in <limits.h>. BITINT_MAXWIDTH bounds N in _BitInt(N) and is at least ULLONG_WIDTH. MB_LEN_MAX is the maximum bytes per multibyte character across all supported locales, not an integer width.

Check INT_MAX before incrementing: signed overflow has undefined behavior. The example avoids it without assuming an int size. A repository C23 compiler flag alone does not attest support for every macro or the production runtime.

Examples

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

static int increment(int *value)
{
    if (*value == INT_MAX) {
        return 0;
    }
    ++*value;
    return 1;
}

int main(void)
{
    int value = INT_MAX - 1;
    int first = increment(&value);
    int second = increment(&value);
    printf("%d %d %d\n", first, second, value == INT_MAX);
    return 0;
}

Result

1 0 1

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