请编写一个c程序确定signed,unsigned的char,short,int和long变量取值范围

Nlf 2007-03-29 09:20:39
way1:
通过打印标准的头文件中的相应的值来完成

way2:
自行通过计算得到

麻烦谁能给出代码?
...全文
996 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨星 2007-03-29
  • 打赏
  • 举报
回复
#include <limits>
#include <iostream>
using namespace std;

template<typename INT_TYPE>
struct to_int {
typedef INT_TYPE int_type;
};

template<>
struct to_int<signed char> {
typedef signed int int_type;
};

template<>
struct to_int<unsigned char> {
typedef unsigned int int_type;
};

template<typename INT_TYPE>
void print_scope() {
cout << typeid(INT_TYPE).name() << ":\t";
cout << (typename to_int<INT_TYPE>::int_type)numeric_limits<INT_TYPE>::min() << " - "
<< (typename to_int<INT_TYPE>::int_type)numeric_limits<INT_TYPE>::max() << endl;
}

int main() {
print_scope<signed char>();
print_scope<unsigned char>();
print_scope<signed short>();
print_scope<unsigned short>();
print_scope<signed int>();
print_scope<unsigned int>();
print_scope<signed long>();
print_scope<unsigned long>();

return 0;
}
thinkinnight 2007-03-29
  • 打赏
  • 举报
回复
还有,这里使用__int64来存取,否则会溢出
thinkinnight 2007-03-29
  • 打赏
  • 举报
回复
signed int的范围,其他和这个差不多。
way1:
#include <climits>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout<<INT_MAX<<endl;
cout<<INT_MIN<<endl;
return 0;
}

way2:

#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
size_t sz = sizeof(int);
__int64 base = 1;
__int64 range = base<<(sz*8-1); //1位符号位,这里打印出最大值,最小值同理
cout<<range<<endl;
return 0;
}

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧