#include <iostream>
using namespace std;
int main()
{
cout << "this is a demo." << endl;
// 布尔型
cout << "bool" << endl;
bool _bool;
cout << _bool << endl;
// signed bool _signed_bool;
// unsigned bool _unsigned_bool;
// short bool _short_bool;
// long bool _long_bool;
cout << sizeof(_bool) << endl; // 占 1 字节
// 字符型
cout << "char" << endl;
char _char;
signed char _signed_char;
unsigned char _unsigned_char;
// short char _short_char;
// long char _long_char;
cout << sizeof(_char) << endl; // 占 1 字节
cout << sizeof(_signed_char) << endl; // 占 1 字节
cout << sizeof(_unsigned_char) << endl; // 占 1 字节
// cout << sizeof(_short_char) << endl; //
// cout << sizeof(_long_char) << endl; //
// 宽字符型
cout << "wchar_t" << endl;
wchar_t _wchar_t;
signed wchar_t _signed_wchar_t;
unsigned wchar_t _unsigned_wchar_t;
short wchar_t _short_wchar_t;
long wchar_t _long_wchar_t;
cout << sizeof(_wchar_t) << endl; // 占 2 字节
cout << sizeof(_signed_wchar_t) << endl; // 占 2 字节
cout << sizeof(_unsigned_wchar_t) << endl; // 占 4 字节
cout << sizeof(_short_wchar_t) << endl; // 占 2 字节
cout << sizeof(_long_wchar_t) << endl; // 占 4 字节
// 短整形
cout << "short" << endl;
short _short;
signed short _signed_short;
unsigned short _unsigned_short;
// short short _short_short;
// long short _long_short;
cout << sizeof(_short) << endl; // 占 2 字节
cout << sizeof(_signed_short) << endl; // 占 2 字节
cout << sizeof(_unsigned_short) << endl; // 占 2 字节
// cout << sizeof(_short_short) << endl; //
// cout << sizeof(_long_short) << endl; //
// 整型
cout << "int" << endl;
int _int;
signed int _signed_int;
unsigned int _unsigned_int;
short int _short_int;
long int _long_int;
cout << sizeof(_int) << endl; // 占 4 字节
cout << sizeof(_signed_int) << endl; // 占 4 字节
cout << sizeof(_unsigned_int) << endl; // 占 4 字节
cout << sizeof(_short_int) << endl; // 占 2 字节
cout << sizeof(_long_int) << endl; // 占 4 字节
// 长整型
cout << "long" << endl;
long _long;
signed long _signed_long;
unsigned long _unsigned_long;
// short long _short_long;
long long _long_long;
cout << sizeof(_long) << endl; // 占 4 字节
cout << sizeof(_signed_long) << endl; // 占 4 字节
cout << sizeof(_unsigned_long) << endl; // 占 8 字节
// cout << sizeof(_short_long) << endl; //
cout << sizeof(_long_long) << endl; // 占 8 字节
// 单精度浮点型
cout << "float" << endl;
float _float;
// signed float _signed_float;
// unsigned float _unsigned_float;
// short float _short_float;
// long float _long_float;
cout << sizeof(_float) << endl; // 占 4 字节
// cout << sizeof(_signed_float) << endl; //
// cout << sizeof(_unsigned_float) << endl; //
// cout << sizeof(_short_float) << endl; //
// cout << sizeof(_long_float) << endl; //
// 双精度浮点型
cout << "double" << endl;
double _double;
// signed double _signed_double;
// unsigned double _unsigned_double;
// short double _short_double;
long double _long_double;
cout << sizeof(_double) << endl; // 占 8 字节
// cout << sizeof(_signed_double) << endl; //
// cout << sizeof(_unsigned_double) << endl; //
// cout << sizeof(_short_double) << endl; //
cout << sizeof(_long_double) << endl; // 占 12 字节
// this is a demo.
// bool
// 1
// char
// 1
// 1
// 1
// wchar_t
// 2
// 2
// 4
// 2
// 4
// short
// 2
// 2
// 2
// int
// 4
// 4
// 4
// 2
// 4
// long
// 4
// 4
// 4
// 8
// float
// 4
// double
// 8
// 12
cout << "Size of bool : " << sizeof(bool) << endl;
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
return 0;
}
C++ 基本数据类型的宽度
最新推荐文章于 2024-02-02 10:25:47 发布
本文详细探讨了C++编程语言中各种基本数据类型的宽度,包括char、int、float、double等。解释了不同平台下这些类型可能的字节数,并讨论了它们在内存中的表示和使用场景。
749

被折叠的 条评论
为什么被折叠?



