数据类型
一、数据类型分类
C++语言中数据类型可以分为:基本数据类型、构造数据类型、指针类型、空类型四大类
二、基本数据类型
基本数据类型主要特点是不能再分解为其他数据类型,分成下面几类
整型
| 名称 | 符号 | 字节数 | 最小值 | 最大值 | 最大位数(十进制) |
| 短整型 | short | 2(16位) | -32768 | 32767 | 5 |
| 整型 | int | 4(32位) | -2147483648 | 2147483647 | 10 |
| 超长整型 | long long | 8(32位) | -9223372036854775808 | 9223372036854775807 | 19 |
| 无符号整型 | unsigned int | 4(32位) | 0 | 4294967295 | 10 |
测试相关代码:
#include <iostream> #include <limits> using namespace std; int main() { cout << "=== 数据类型大小验证 ===" << endl; // 字节大小 cout << "short字节数: " << sizeof(short) << " 字节" << endl; cout << "int字节数: " << sizeof(int) << " 字节" << endl; cout << "long long字节数: " << sizeof(long long) << " 字节" << endl; cout << "unsigned int字节数: " << sizeof(unsigned int) << " 字节" << endl; cout << "\n=== 数值范围 ===" << endl; // 数值范围 cout << "short 范围: " << numeric_limits<short>::min() << " 到 " << numeric_limits<short>::max() << endl; cout << "int 范围: " << numeric_limits<int>::min() << " 到 " << numeric_limits<int>::max() << endl; cout << "long long 范围: " << numeric_limits<long long>::min() << " 到 " << numeric_limits<long long>::max() << endl; cout << "unsigned int 范围 " <<numeric_limits<unsigned int>::min() << " 到 " << numeric_limits<unsigned int>::max() << endl; cout << "\n=== 位宽 ===" << endl; // 位宽(字节数 × 8) cout << "short 位宽: " << sizeof(short) * 8 << " 位" << endl; cout << "int 位宽: " << sizeof(int) * 8 << " 位" << endl; cout << "long long 位宽: " << sizeof(long long) * 8 << " 位" << endl; cout << "unsigned int 位宽: " << sizeof(unsigned int) * 8 << " 位" << endl; return 0; }2.字符型
| 名称 | 符号 | 字节数 |
| 字符 | char | 1(8位) |
相关代码:
#include <iostream> #include <limits> using namespace std; int main() { cout << "=== 数据类型大小验证 ===" << endl; // 字节大小 cout << "char字节数: " << sizeof(char) << " 字节" << endl; cout << "\n=== 位宽 ===" << endl; // 位宽(字节数 × 8) cout << "char位宽: " << sizeof(char) * 8 << " 位" << endl; return 0; }3.浮点型
| 名称 | 符号 | 字节数 | 最小值 | 最大值 |
| 单精度浮点型 | float | 4(32位) | 1.17549e-038 | 3.40282e+038 |
| 双精度浮点型 | double | 8(64位) | 2.22507e-308 | 1.79769e+308 |
参考代码:
#include <iostream> #include <limits> using namespace std; int main() { cout << "=== 数据类型大小验证 ===" << endl; // 字节大小 cout << "float字节数: " << sizeof(float) << " 字节" << endl; cout << "double字节数: " << sizeof(double) << " 字节" << endl; cout << "\n=== 数值范围 ===" << endl; // 数值范围 cout << "float 范围: " << numeric_limits<float>::min() << " 到 " << numeric_limits<short>::max() << endl; cout << "double 范围: " << numeric_limits<double>::min() << " 到 " << numeric_limits<int>::max() << endl; cout << "\n=== 位宽 ===" << endl; // 位宽(字节数 × 8) cout << "float 位宽: " << sizeof(float) * 8 << " 位" << endl; cout << "double 位宽: " << sizeof(double) * 8 << " 位" << endl; return 0; }二、构造数据类型
构造数据类型是根据已经定义的一个或多个数据类型用构造的方法来定义的,也就是说,一个构造类型的值可以分解成若干个“成员”或”元素“。每个”成员“都是一个基本数据类型或又是一个构造类型。
(1)数组类型
例如定义语句“int a[10];”表示a是一个整型数组,长度为10,从 a[0]到 a[9]共10个数组元素。在内存中开辟了连续10个int类型数据的空间,如果int类型数据占4个字节,那就是开辟了连续的40个字节的空间来存储10个整型数组元素。数组可分为一维数组、二维数组和多维数组。
(2)结构体类型
它和数组类型的相同点是它也是将多个数据定义成一个整体,不同点是结构体类型的成员可以是不同数据类型的数据。如下例中定义了一种新的结构体类型即struct person 类型,它的成员变量 name 数组、age、sex 等分属不同的数据类型。
stuct person{ char name[10]; int age; char sex; };(3)共用体类型
union Data { int i; float f; char str[20]; };三、指针类型
指针类型是一种特殊的、具有重要作用的数据类型,其特殊性体现在指针的值代表某个变量在内存中的地址。虽然指针变量的取值类似于整型变量,但是它们的两个类型完全不同的量。
四、空类型
空类型即void类型,经常用于表示函数返回值的类型。
.jztagtree{max-height:85vh;right:0px}.jzDown{top:10vh}.jztagtree li a{background-color:#448EF6}.jztagtree li a:before{border-right:10px solid #448EF6}.jztagtree li a:hover{background:#0045a6}.jztagtree li a:hover::before{border-right:10px solid #0045a6}
$("#jztoc").toc({content: ".single", headings: "h1,h2,h3"});