← 返回首页
C++基础教程(二)
发表时间:2023-07-21 15:38:04
数据类型

与Java类似,C++的数据类型分为两大类:基本类型和引用类型。引用类型(也称为复合类型)包括数组、字符串、指针、结构。而基本类型则包括 整型和浮点型。

1.C++数据类型

2.基本数据类型

C++ 并没有统一规定各类数据的精度、数值范围和在内存中所占的字节数,各种C++ 编译系统根据自己的情况作出安排。只规定了int型数据所占的字节数不大于long型,不小于short型。一般在16位机的C++ 系统,短整型(short)和整型(int)只占两个字节,长整型(long)占4个字节 。在Visual C++ 6.0中,短整型占两个字节,整型和长整型占4个字节。

查看基本数据类型占用字节情况,如下代码所示:

#include <iostream>

using namespace std;

int main()
{
   cout << "Size of char : " << sizeof(char) << 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;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;
}

运行结果:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 2