Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

<stdckdint.h> — checked integer arithmetic

The C23 header <stdckdint.h> provides addition, subtraction, and multiplication that detect results outside the destination type's range.

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

Syntax

#include <stdckdint.h>
ckd_add(&result, a, b)
ckd_sub(&result, a, b)
ckd_mul(&result, a, b)

ckd_add, ckd_sub, and ckd_mul store through a pointer. They return false when the result fits the destination type, and true otherwise.

Computation uses mathematical operand values; an out-of-range stored result wraps to the destination width. Types may differ. Plain char, bool, enumeration types, and bit-precise types are excluded. The destination must be modifiable.

The macro __STDC_VERSION_STDCKDINT_H__ is 202311L. Check header availability in your C23 implementation.

Examples

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

int main(void)
{
    int result;
    if (!ckd_add(&result, 20, 22)) {
        printf("sum=%d\n", result);
    }
    if (ckd_add(&result, INT_MAX, 1)) {
        puts("overflow");
    }
    return 0;
}

Result

sum=42
overflow

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