#C 语言标准库头文件 stdckdint.h
这个头文件提供 整数运算溢出检查 的相关功能。
#函数
#推荐阅读
#示例
#include <stdio.h>
#include <limits.h>
#include <stdckdint.h>
int main(void) {
int a, b, result;
// 正常情况
a = 100;
b = 200;
if (ckd_add(&result, a, b)) {
printf("%d + %d = %d 结果溢出了!\n", a, b, result);
} else {
printf("%d + %d = %d\n", a, b, result); // 输出: 100 + 200 = 300
}
// 溢出情况
a = INT_MAX;
b = 1;
if (ckd_add(&result, a, b)) {
printf("%d + %d = %d 结果溢出了!\n", a, b, result);
} else {
printf("%d + %d = %d\n", a, b, result);
}
// 乘法示例
a = 100000;
b = 100000;
if (ckd_mul(&result, a, b)) {
printf("%d * %d = %d 结果溢出了!\n", a, b, result);
} else {
printf("%d * %d = %d\n", a, b, result);
}
return 0;
}
运行结果:
100 + 200 = 300 2147483647 + 1 = -2147483648 结果溢出了! 100000 * 100000 = 1410065408 结果溢出了!