与OS、编译器有关,比如同样是在32位操作系统,VC++的编译器下int类型为4个字节,而在tuborC下则是2个字节。
#include<iostream>
using namespace std;
int main()
{
cout << "int类型占用的字节数:" << sizeof(int) << endl;
cout << "short int类型占用的字节数:" << sizeof(short int) << endl;
cout << "unsigned int类型占用的字节数:" << sizeof(unsigned int) << endl;
cout << "long int类型占用的字节数:" << sizeof(long) << endl;
cout << "long long类型占用的字节数:" << sizeof(long long) << endl;
cout << "float类型占用的字节数:" << sizeof(float) << endl;
cout << "double类型占用的字节数:" << sizeof(double) << endl;
cout << endl << "下面是指针类型!" << endl;
cout << "int*类型占用的字节数:" << sizeof(int*) << endl;
cout << "short int*类型占用的字节数:" << sizeof(short int*) << endl;
cout << "long int*类型占用的字节数:" << sizeof(long*) << endl;
cout << "double*类型占用的字节数:" << sizeof(double*) << endl;
cout << "char*类型占用的字节数:" << sizeof(char*) << endl;
}
我在VS2017下运行的结果如下:
long long 占用8个字节。 8*8=64bit。最大表示数为2^63。最多19位。
可以得到指针类型都占8个字节。