首页 > 技术文章 > c c++各种类型的取值范围

didiaodidiao 2018-06-06 22:04 原文

int类型的变量存储值从-2147483648到2147483647

1 //例子
2 #include <iostream>
3 2 using namespace std;
4 3 int main(void)
5 4 {
6 5 
7 6     cout<<"int类型的取值范围为:"<<INT_MIN<<""<<INT_MAX<<endl;
8 7     return 0;
9 8 }

unsigned int类型的变量存储值从0到4294967295

1 //例子
2 #include <iostream>
3 using namespace std;
4 int main(void)
5 {
6     cout<<"unsigned int类型的取值范围为:0到"<<UINT_MAX<<endl;
7     return 0;
8 }

short类型的变量存储值从-32768到32767

unsigned short类型的变量存储值从0到65535


char类型的变量存储值从-128到127
unsigned char类型的变量存储值从0到255


long类型的变量存储值从-2147483648到2147483647
unsigned long类型的变量存储值从0到4294967295
long long类型的变量存储值从-9223372036854775808到9223372036854775807
unsigned long long类型的变量存储值从0到18446744073709551615

 

最小的非零float类型变量的值的是1.175e-038
最大的float类型变量的值的是3.403e+038
最小的非零double类型变量的值的是2.225e-308
最大的double类型变量的值的是1.798e+308
最小的非零long double类型变量的值的是-0.000e+000
最大的long double类型变量的值的是-1.#QOe+000
float类型的变量提供6位精度的小数位数
double类型的变量提供15位精度的小数位数
long double类型的变量提供18位精度的小数位数

 

 1 #include <stdio.h>
 2 #include <limits.h>
 3 #include <float.h>
 4 #include <stdlib.h>
 5 int main(void)
 6 
 7 {
 8 
 9     printf("char类型的变量存储值从%d到%d\n", CHAR_MIN, CHAR_MAX);
10 
11     printf("unsigned char类型的变量存储值从0到%u\n", UCHAR_MAX);
12 
13     printf("short类型的变量存储值从%d到%d\n", SHRT_MIN, SHRT_MAX);
14 
15     printf("unsigned short类型的变量存储值从0到%u\n", USHRT_MAX);
16 
17     printf("int类型的变量存储值从%d到%d\n", INT_MIN, INT_MAX);
18 
19     printf("unsigned int类型的变量存储值从0到%u\n", UINT_MAX);
20 
21     printf("long类型的变量存储值从%ld到%ld\n", LONG_MIN, LONG_MAX);
22 
23     printf("unsigned long类型的变量存储值从0到%lu\n\n", ULONG_MAX);
24 
25     printf("long long类型的变量存储值从%lld到%lld\n", LLONG_MIN, LLONG_MAX);
26 
27     printf("unsigned long long类型的变量存储值从0到%llu\n", ULLONG_MAX);
28 
29     printf("最小的非零float类型变量的值的是%.3e\n", FLT_MIN);
30 
31     printf("最大的float类型变量的值的是%.3e\n", FLT_MAX);
32 
33     printf("最小的非零double类型变量的值的是%.3e\n", DBL_MIN);
34 
35     printf("最大的double类型变量的值的是%.3e\n\n", DBL_MAX);
36 
37     printf("最小的非零long double类型变量的值的是%.3Le\n", LDBL_MIN);
38 
39     printf("最大的long double类型变量的值的是%.3Le\n", LDBL_MAX);
40 
41     printf("float类型的变量提供%u位精度的小数位数\n", FLT_DIG);
42 
43     printf("double类型的变量提供%u位精度的小数位数\n\n", DBL_DIG);
44 
45     printf("long double类型的变量提供%u位精度的小数位数\n", LDBL_DIG);
46 
47     system("pause");
48 
49     return 0;
50 }

 

 1 #include<iostream>
 2 #include<string>
 3 #include <limits>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8 
 9     cout << "type: \t\t" << "************size**************"<< endl;
10     cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
11     cout << "\t最大值:" << (numeric_limits<bool>::max)();
12     cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
13     cout << "char: \t\t" << "所占字节数:" << sizeof(char);
14     cout << "\t最大值:" << (numeric_limits<char>::max)();
15     cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
16     cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
17     cout << "\t最大值:" << (numeric_limits<signed char>::max)();
18     cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
19     cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
20     cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
21     cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
22     cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
23     cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
24     cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
25     cout << "short: \t\t" << "所占字节数:" << sizeof(short);
26     cout << "\t最大值:" << (numeric_limits<short>::max)();
27     cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
28     cout << "int: \t\t" << "所占字节数:" << sizeof(int);
29     cout << "\t最大值:" << (numeric_limits<int>::max)();
30     cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
31     cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
32     cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
33     cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
34     cout << "long: \t\t" << "所占字节数:" << sizeof(long);
35     cout << "\t最大值:" << (numeric_limits<long>::max)();
36     cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
37     cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
38     cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
39     cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
40     cout << "double: \t" << "所占字节数:" << sizeof(double);
41     cout << "\t最大值:" << (numeric_limits<double>::max)();
42     cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
43     cout << "long double: \t" << "所占字节数:" << sizeof(long double);
44     cout << "\t最大值:" << (numeric_limits<long double>::max)();
45     cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
46     cout << "float: \t\t" << "所占字节数:" << sizeof(float);
47     cout << "\t最大值:" << (numeric_limits<float>::max)();
48     cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
49     cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
50     cout << "\t最大值:" << (numeric_limits<size_t>::max)();
51     cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
52     cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
53     // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
54     cout << "type: \t\t" << "************size**************"<< endl;
55     return 0;
56 }

 

推荐阅读