最近看到这方面的知识,众说纷纭,所以自己动手实验下。请看代码:
#include<iostream>
using namespace std;
int main()
{
cout << "字符数据:" << endl;
cout << "char\t" << sizeof(char) << endl;
cout << "unsigned char\t" << sizeof(unsigned char) << endl;
cout << "char*\t" << sizeof(char*) << endl << endl;
cout << "整型数据:" << endl;
cout << "int\t" << sizeof(int) << endl;
cout << "unsigned int\t" << sizeof(unsigned int) << endl;
cout << "short\t" << sizeof(short) << endl;
cout << "unsigned short\t" << sizeof(unsigned short) << endl;
cout << "long\t" << sizeof(long) << endl;
cout << "unsigned long\t" << sizeof(unsigned long) << endl;
cout << "long long\t" << sizeof(long long) << endl << endl;
cout << "浮点数据:" << endl;
cout << "float\t" << sizeof(float) << endl;
cout << "double\t" << sizeof(double) << endl;
cout << "long double\t" << sizeof(long double) << endl << endl;
cout << "字符串数据:" << endl;
cout << "string\t" << sizeof(string) << endl;
}
测试环境是Visual Studio 2013,首先代码是在默认的32位系统下运行,结果如下图:
然后调整Visual Studio 项目属性,切换到64位系统下运行程序,过程如下图:
这里切换32位系统为64位:
切换完成后重新运行程序,结果如下图:
对比下我们就可以知道32位系统和64位系统下,不同类型数据所占字节数的差异!