C++常用语法知识--数据类型
C++常用语法知识--数据类型
C++为用户提供了7种基本C++数据类型:
| 类型 | 关键字 | 字节大小 |
|---|---|---|
| 布尔型 | bool | 1 |
| 字符型 | char | 1 |
| 有符号字符型 | signed char | 1 |
| 无符号字符型 | unsigned char | 1 |
| 整型 | int | 4 |
| 有符号整型 | signed int | 4 |
| 无符号整型 | unsigned int | 4 |
| 短整型 | short int | 2 |
| 长整型 | long int | 4 |
| 有符号长整型 | signed long int | 4 |
| 无符号长整型 | unsigned long int | 4 |
| 有符号短整型 | signed int | 2 |
| 无符号短整型 | unsigned int | 2 |
| 浮点型 | float | 4 |
| 双浮点型 | double | 8 |
| 长双浮点型 | long double | 16 |
| 无类型 | void | 1 |
| 宽字符型 | wchar_t | 2 |
代码
#include <iostream>
using namespace std;
int main(){
cout <<"Size of bool:"<< sizeof(bool)<<endl;
cout <<"Size of char:"<< sizeof(char)<<endl;
cout <<"Size of signed char:"<< sizeof(signed char)<<endl;
cout <<"Size of unsigned char:"<< sizeof(unsigned char)<<endl;
cout <<"Size of int:"<<sizeof(int)<<endl;
cout <<"Size of signed int:"<<sizeof(signed int)<<endl;
cout <<"Size of unsigned int:"<<sizeof(unsigned int)<<endl;
cout <<"Size of short int:" <<sizeof(short int)<<endl;
cout <<"Size of signed short int:"<<sizeof(signed short int)<<endl;
cout <<"Size of unsigned short int:"<<sizeof(unsigned short int)<<endl;
cout << "Size of long int:" <<sizeof(long int)<<endl;
cout <<"Size of signed long int:"<<sizeof(signed long int)<<endl;
cout <<"Size of unsigned long int:"<<sizeof(unsigned long int)<<endl;
cout<<"Size of float:"<<sizeof(float)<<endl;
cout<<"Size of double:"<<sizeof(double)<<endl;
cout<<"Size of long double:"<<sizeof(long double)<<endl;
cout<<"Size of wchar_t:"<<sizeof(wchar_t)<<endl;
return 0;
}
结果显示:


浙公网安备 33010602011771号