基本数据类型在32位系统和64位系统中所占字节数

来源:互联网 发布:手机识图软件 编辑:程序博客网 时间:2024/05/20 05:28

最近看到这方面的知识,众说纷纭,所以自己动手实验下。请看代码:

#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位系统下,不同类型数据所占字节数的差异!

在Linux环境中进行测试结果如下,测试环境分别为32位ubuntu和64位centos:

这里写图片描述


这里写图片描述


从中看到long型数据在64位系统中占8字节,与VisualStudio的结果不同,应特别注意。一般题目应该特指Linux环境下,需多加注意!

0 0