码迷,mamicode.com
首页 > 编程语言 > 详细

C++之数据类型及其值的范围

时间:2016-06-16 21:33:00      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

  C++中的常用类型有bool、char、short、int、long、double、float、string等。这些数据类型满足了我们日常变成的需求。

  下面的代码用来打印出各数据类型对应的占字节数及其最大值和最小值。

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

  程序的运行结果如下图所示。

技术分享

  从程序运行的结果可以得出以下结论:

  • bool只有两个值,即true和false,在C++语言中可以分别用1和0来表示
  • 能表示小数的数据类型有三种:double、long double和float,其值分别可以占8位、16位和4位
  • 范围最大的整数数据类型是long long,在ACM大赛中常用来处理大数字

  对上述结果做如下补充解释:

  • wchar_t是C++中的一种8位的字符类型,其能表示的范围远超char
  • size_t是sizeof标识符返回的数据的类型,通常用于循环、数组索引、大小的存储和地址运算。其长度虽然与long long类型相同,但基本不会出现在程序中表示一个数字变量

C++之数据类型及其值的范围

标签:

原文地址:http://www.cnblogs.com/blog-wzy/p/5592088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!