|
类型 |
字节数(byte) |
取值范围 |
近似值 |
整型 |
int |
4B=32bit |
-231~(231-1) |
绝对值在109以内/32位整数 |
long |
||||
long long |
8B=64bit |
-263~(263-1) |
绝对值在109~1018/64位整数 |
|
__int64 |
||||
浮点型 |
float 单精度 |
4B=32bit |
-2128~2128 |
有效精度(也就是小数点后)只有6~7位 |
double 双精度 |
16B=64bit |
-21024~21024 |
有效精度(也就是小数点后)只有15~16位 |
|
字符型 |
char |
2B=8bit |
-128~127 |
|
布尔值 |
bool |
1 #include<iostream> 2 #include<string> 3 #include <limits> 4 using namespace std; 5 6 int main() 7 { 8 cout << "type: \t\t" << "************size**************"<< endl; 9 cout << "bool: \t\t" << "所占字节数:" << sizeof(bool); 10 cout << "\t最大值:" << (numeric_limits<bool>::max)(); 11 cout << "\t\t\t最小值:" << (numeric_limits<bool>::min)() << endl; 12 cout << "char: \t\t" << "所占字节数:" << sizeof(char); 13 cout << "\t最大值:" << (numeric_limits<char>::max)(); 14 cout << "\t\t\t最小值:" << (numeric_limits<char>::min)() << endl; 15 cout << "short: \t\t" << "所占字节数:" << sizeof(short); 16 cout << "\t最大值:" << (numeric_limits<short>::max)(); 17 cout << "\t\t\t最小值:" << (numeric_limits<short>::min)() << endl; 18 cout << "int: \t\t" << "所占字节数:" << sizeof(int); 19 cout << "\t最大值:" << (numeric_limits<int>::max)(); 20 cout << "\t\t最小值:" << (numeric_limits<int>::min)() << endl; 21 cout << "long: \t\t" << "所占字节数:" << sizeof(long); 22 cout << "\t最大值:" << (numeric_limits<long>::max)(); 23 cout << "\t\t最小值:" << (numeric_limits<long>::min)() << endl; 24 cout << "long long: \t" << "所占字节数:" << sizeof(long long); 25 cout << "\t最大值:" << (numeric_limits<long long>::max)(); 26 cout << "\t最小值:" << (numeric_limits<long long>::min)() << endl; 27 cout << "__int64: \t" << "所占字节数:" << sizeof(__int64); 28 cout << "\t最大值:" << (numeric_limits<__int64>::max)(); 29 cout << "\t最小值:" << (numeric_limits<__int64>::min)() << endl; 30 cout << "double: \t" << "所占字节数:" << sizeof(double); 31 cout << "\t最大值:" << (numeric_limits<double>::max)(); 32 cout << "\t\t最小值:" << (numeric_limits<double>::min)() << endl; 33 cout << "float: \t\t" << "所占字节数:" << sizeof(float); 34 cout << "\t最大值:" << (numeric_limits<float>::max)(); 35 cout << "\t\t最小值:" << (numeric_limits<float>::min)() << endl; 36 cout << "type: \t\t" << "************size**************"<< endl; 37 return 0; 38 }
代码及运行结果
Question:
l int 与 long 的区别
long 最少不低于32bit
l long 与 long long 的区别
long类似于int
l long long 与 __int64 的区别
在数值表示上没差别
———————————————————————————————————————————————
需要注意的是,如果long long型赋绝对值大于231-1的初值时,需要在初值后面加上“LL”。
例如:long long bigNum=1234567890123LL;
OJ上通常使用g++编译器,其64位扩展方式与VC有所不同,采用的是long long类型。
对于long long bigNum;
C++写法 |
C写法 |
cin>> bigNum; cout<< bigNum; |
scanf(“%lld”,& bigNum); printf(“%lld”, bigNum); |
参考: