C++数据类型总结

关键字:C++, 数据类型, VS2015。

OS:Windows 10。

 

ANSI C/C++基本数据类型:

Type

Size

数值范围

无值型void

0 byte

无值域

布尔型bool

1 byte

true   false

有符号短整型short [int] /signed short [int]

2 byte

-32768~32767

无符号短整型unsigned short [int]    

2 byte

0~65535

有符号整型int /signed [int]

4 byte

-2147483648~2147483647

无符号整型unsigned [int]

4 byte

0~4294967295

有符号长整型long [int]/signed long [int]

4 byte

-2147483648~2147483647

无符号长整型unsigned long [int]

4 byte

0~4294967295

long long

8 byte

0~18446744073709552000

有符号字符型char/signed char

1 byte

-128~127

无符号字符型unsigned char

1 byte

0~255

宽字符型wchar_t (unsigned short.)

2 byte

0~65535

单精度浮点型float

4 byte

-3.4E-38~3.4E+38

双精度浮点型double

8 byte

1.7E-308~1.7E+308

long double

8 byte

 

指针(char*)

4 byte/8byte

32bit的应用程序指针是4byte。

64bit的应用程序指针是8byte。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

注:

(1)类型修饰符signed和unsigned用于修饰字符型和整形。

(2)类型修饰符short和long用于修饰字符型和整形。

(3)当用signed和unsigned、short和long修饰int整形时,int可省略。

(4)其中bool和wchar_t是C++特有的。对于条件判断,零为假,非零为真,对bool变量可赋非0非1的其他真值。

(5)float的精度(6位有效数字)通常是不够的,double类型可以保证10位有效数字,能够满足大多数计算的需要。使用double类型基本不会出错,在float类型中存在隐式的精度损失。默认的浮点字面值常量为double类型,在数值后面加上F或f表示单精度,例如3.14159F。浮点数float、double的存储设计,从本质上来说是设计了一个数值映射,充分利用了二进制存储的特点。参考IEEE754浮点数表示标准。

(6)指针在32bit应用程序里大小是4byte,在64bit应用程序里面大小是8byte。

-----------------------------------------------------------------------------------------------------------------

下面用VS2015创建一个Win32控制台工程来检验各数据类型的大小,代码如下:

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "sizeof(bool)" << sizeof(bool) << endl;
    cout << "sizeof(short )" << sizeof(short) << endl;
    cout << "sizeof(signed short)" << sizeof(signed short) << endl;
    cout << "sizeof(unsigned short)" << sizeof(unsigned short) << endl;
    cout << "sizeof(int)" << sizeof(int) << endl;
    cout << "sizeof(unsigned int)" << sizeof(unsigned int) << endl;
    cout << "sizeof(long)" << sizeof(long) << endl;
    cout << "sizeof(unsigned long)" << sizeof(unsigned long) << endl;
    cout << "sizeof(long long)" << sizeof(long long) << endl;
    cout << "sizeof(char)" << sizeof(char) << endl;
    cout << "sizeof(unsigned char)" << sizeof(unsigned char) << endl;
    cout << "sizeof(wchar_t)" << sizeof(wchar_t) << endl;
    cout << "sizeof(float)" << sizeof(float) << endl;
    cout << "sizeof(double)" << sizeof(double) << endl;
    cout << "sizeof(long double)" << sizeof(long double) << endl;
    cout << "sizeof(char*)" << sizeof(char*) << endl;

    return 0;
}
sizeof是C/C++中的一个操作符(operator),简单的说其作用就是返回一个对象或者类型所占的内存字节数。
MSDN上的解释为:
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types). This keyword returns a value of type size_t.
 
运行结果(32bit):

sizeof(bool)1
sizeof(short )2
sizeof(signed short)2
sizeof(unsigned short)2
sizeof(int)4
sizeof(unsigned int)4
sizeof(long)4
sizeof(unsigned long)4
sizeof(long long)8
sizeof(char)1
sizeof(unsigned char)1
sizeof(wchar_t)2
sizeof(float)4
sizeof(double)8
sizeof(long double)8
sizeof(char*)4

运行结果(64bit):

sizeof(bool)1
sizeof(short )2
sizeof(signed short)2
sizeof(unsigned short)2
sizeof(int)4
sizeof(unsigned int)4
sizeof(long)4
sizeof(unsigned long)4
sizeof(long long)8
sizeof(char)1
sizeof(unsigned char)1
sizeof(wchar_t)2
sizeof(float)4
sizeof(double)8
sizeof(long double)8
sizeof(char*)8

 

posted @ 2015-09-10 20:42  Ldlchina  阅读(543)  评论(0编辑  收藏  举报