Namespaces
Variants

ckd_sub

From cppreference.com
Defined in header <stdckdint.h>
#define ckd_sub( result, a, b ) /* implementation-defined */
// exposed interface:
bool ckd_sub( type1* result, type2 a, type3 b );
(since C23)

Computes the subtraction x - y and stores the result into *result. The subtraction is performed as if both operands were represented in a signed integer type with infinite range, and the result was then converted from this integer type to type1. If the value assigned to *result correctly represents the mathematical result of the operation, it returns false. Otherwise, it returns true. In this case, the value assigned to *result is the mathematical result of the operation wrapped around to the width of *result.

Parameters

a, b - integer values
result - address of where result should be stored

Return value

false if the value assigned to *result correctly represents the mathematical result of the subtraction, true otherwise.

Note

Both type2 and type3 shall be any integer type other than “plain” char, bool, a bit-precise integer type, or an enumerated type, and they can be the same. *result shall be a modifiable lvalue of any integer type other than “plain” char, bool, a bit-precise integer type, or an enumerated type.

It is recommended to produce a diagnostic message if type2 or type3 are not suitable integer types, or if *result is not a modifiable lvalue of a suitable integer type.

Example

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

int main()
{
    int16_t result1;
    constexpr int8_t x1 = -2;
    constexpr int16_t y1 = 1;
    const bool underflow1 = ckd_sub(&result1, x1, y1);
    printf("%i - %i == %i (%s)\n", x1, y1, result1, underflow1 ? "Underflow" : "OK");

    int16_t result2;
    constexpr int8_t x2 = -2;
    constexpr int16_t y2 = INT16_MAX;
    const bool underflow2 = ckd_sub(&result2, x2, y2);
    printf("%i - %i == %i (%s)\n", x2, y2, result2, underflow2 ? "Underflow" : "OK");

    int32_t result3;
    constexpr int8_t x3 = -2;
    constexpr int16_t y3 = INT16_MAX;
    const bool underflow3 = ckd_sub(&result3, x3, y3);
    printf("%i - %i == %i (%s)\n", x3, y3, result3, underflow3 ? "Underflow" : "OK");
}

Output:

-2 - 1 == -3 (OK)
-2 - 32767 == 32767 (Underflow)
-2 - 32767 == -32769 (OK)

References

  • C23 standard (ISO/IEC 9899:2024):
  • 7.20.1 The ckd_ checked integer operation macros (p: 311)

See also

checked addition operation on two integers
(type-generic function macro)[edit]
checked multiplication operation on two integers
(type-generic function macro)[edit]
C++ documentation for ckd_sub
Morty Proxy This is a proxified and sanitized view of the page, visit original site.