数据类型的归纳

来源:互联网 发布:图章设计软件 编辑:程序博客网 时间:2024/06/05 13:25

      今天偶然测试了很简单的程序,就是 int 型二个数的和,突然发现我输入一个很大很大的值它都不会溢出,可能是因为以前被单片机的程序所迷惑,一直以为 int 的 最大也就32767 ,即使是无符号 int  也就 65535,为什么在Visual我测试的那个程序可以取如此之大的数都不会溢出,翻开C++的书,猛然发现原来在Visual C++ 里  int 最大值是2147783647。我就在想是不是以前的Keil编译器默认的int 型是 2个字节的呢?后来发现不是这样的。但是现在还不知道为什么当初单片机里面的 int 就被思想固化为 2个字节的。

      下面是我在Visual C++ 里面测试的结果:

#include<iostream>
using namespace std;
int main(void)
{
    unsigned char a,b,c,d,e,f,g;
    cout << "Various types in the share of the number of bytes Visual C++:" <<endl;
    cout << "a = " << sizeof(char) << endl;
    cout << "b = " << sizeof(short int) << endl;
    cout << "c = " << sizeof(int) << endl;
    cout << "d = " << sizeof(long) << endl;
    cout << "e = " << sizeof(long int) << endl;
    cout << "f = " << sizeof(float) << endl;
    cout << "g = " << sizeof(double) << endl;
    return 0;
}

 

下面是Visual C++ 数值型和字符型数据的统计情况

 

类型                        类型标识符               字节                       数值范围

————————————————————————————————————

整型                        [signed] int                  4           -2147483648 ~ +2147483647

————————————————————————————————————

无符号整型           unsigned [int]                4                       0~4294967295

————————————————————————————————————

短整型                      short [int]                   2                    -32768 ~ +32767

————————————————————————————————————

无符号短整型       unsigned short  [int]       2                        0 ~ 65535

————————————————————————————————————

长整型                      long [int]                    4            -2147483648 ~ +2147483647   

———————————————————————————————————— 

无符号长整型       unsigned long [int]        4                       0~4294967295

————————————————————————————————————

字符型                    [signed]  char              1                            -128 ~ +127

————————————————————————————————————

无符号字符型        unsigned char              1                              0 ~ 255

————————————————————————————————————

单精度型                    float                         4                3.4×10^-38~3.4×10^38

————————————————————————————————————

双精度型                  double                       8                1.7×10^-308 ~ 1.7×10^308

————————————————————————————————————

长双精度型            long double                  8               1.7×10^-308 ~ 1.7×10^308

————————————————————————————————————

       整型数据的储存方式为按二进制数形式存储,如果指定为 signed ,则数值以补码形式存放,存储单元中的最高位(bit)用来表示数值的符号;如果指定为 unsigned ,则数值没有符号,全部二进制位都用来表示数值本身。

      浮点型又叫实型数据分为单精度(float) 、双精度(double) 和长双精度(long double)3种,在Visual C++ 6.0 里,对 float 提供6位有效数字,对 double 提供 15位有效数字。

       上表中,方括号 [ ] 包含的部分可以省略写,如:short 和 short  int 等效,  unsigned  int  和 unsigned  等效。 

原创粉丝点击