gpt4 book ai didi

c++ - 测试整数加法是否溢出

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:14:23 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Best way to detect integer overflow in C/C++

我已经尝试实现一个简单的程序来测试在整数加法过程中是否发生溢出:

#include <climits>
#include <iostream>
#include <string>

using namespace std;

string overflow(long a,long b){
return ((a+b)>UINT_MAX)?"true":"false";
}

int main(){
long a, b;
cout << "enter a and b: ";
cin >> a >> b;
string m = overflow(a,b);
cout << m;

return 0;
}

UINT_MAX=65535 所以我输入了 65535 和 20 但它写错了为什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com