1.用sizeof()运算符查看各数据类型大小
注意以下内部定义的别名:
unsigned short = unit16_t
unsigned int = uint32_t
unsigned long (long) = uint64_t
#include<iostream>
#include<stdio.h>
using namespace std;
int main() {
cout << "char : " << sizeof(char) << "字节" << endl;
cout << "unsigned char : " << sizeof(uint8_t) << "字节" << endl;
cout << "w_char : " << sizeof(wchar_t) << "字节" << endl;
cout << "char16_t : " << sizeof(char16_t) << "字节" << endl;
cout << "char32_t : " << sizeof(char32_t) << "字节" << endl;
cout << "short : " << sizeof(short) << "字节" << endl;
cout << "unsigned short : " << sizeof(uint16_t) << "字节" << endl;
cout << "int : " << sizeof(int) << "字节" << endl;
cout << "unsigned int : " << sizeof(uint32_t) << "字节" << endl;
cout << "long : " << sizeof(long) << "字节" << endl;
cout << "long long : " << sizeof(long long) << "字节" << endl;
cout << "unsigned long (long) : " << sizeof(uint64_t) << "字节" << endl;
cout << "float : " << sizeof(float) << "字节" << endl;
cout << "double : " << sizeof(double) << "字节" << endl;
cout << "long double : " << sizeof(long double) << "字节" << endl;
return 0;
}
2.分别在VS 和 Linux 环境下运行代码并对比结果
3.总结:
类型 | Windows | Ubuntu18 |
char | 1 | 1 |
unsigned char | 1 | 1 |
short | 2 | 2 |
unsigned short | 2 | 2 |
int | 4 | 4 |
unsigned int | 4 | 4 |
long | 4 | 8 |
long long | 8 | 8 |
unsigned long (long) | 8 | 8 |
float | 4 | 4 |
double | 8 | 8 |
long double | 8 | 16 |
w_char | 2 | 4 |
char16_t | 2 | 2 |
char32_t | 4 | 4 |
此外,32位系统的指针大小为4个字节,64位系统的指针大小为8个字节。