doc/docs/CPlusPlus-main/08_C++的数据类型.md
sairate fa9377e4ae docs(book): 添加现代 C++教程及相关代码
- 新增现代 C++ 教程的 Preface 章节,包括英文和中文版本
- 添加 C++ Primer 练习代码
- 新增 Learn C++ 教程的 C++ 开发简介章节
- 添加头文件解析文档
- 更新 mkdocs.yml,包含新教程的目录结构
- 修改项目设置,使用 Python 3.10环境
2025-07-08 09:52:45 +08:00

5.9 KiB
Raw Blame History

C++的数据类型

1. 数据类型

1.1 整形

① 作用:整型变量表示的是整数类型的数据。

② C++中能够表示整形的类型有以下几种方式,区别在于所占内存空间不同。

③ 数据类型 占用空间 取值范围

  1. short(短整型) 2字节 -2^15-2^15-1
  2. int整型 4字节 -2^31-2^31-1
  3. long(长整型) Windows为4字节Linux为4字节(32位)8字节(64位) -2^31-2^31-1
  4. long long(长长整型) 8字节 -2^63-2^63-1
#include <iostream>
using namespace std;

int main()
{
    //1短整型(-32768~32767)
    short num1 = 10;
    short num5 = 32768; //溢出了打印出来是-23768
    short num6 = 32770; //溢出了打印出来是-32766

    //2整型
    int num2 = 10;

    //3长整型
    long num3 = 10;

    //4长长整型
    long long num4 = 10;

    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;
    cout << "num3 = " << num3 << endl;
    cout << "num4 = " << num4 << endl;
    cout << "num5 = " << num5 << endl;
    cout << "num6 = " << num6 << endl;

    system("pause");

    return 0;

}

运行结果:

  • num1 = 10
  • num2 = 10
  • num3 = 10
  • num4 = 10
  • num5 = -32768
  • num6 = -32766
  • 请按任意键继续. . .

1.2 实型(浮点型)

① 作用:用于表示小数。

② 浮点型变量分为下面两种,两个的区别在于表示有效数字范围不同。

  1. 单精度float
  2. 双精度double

③ 数据类型 占用字节 有效数字范围

  1. float 4字节 7位有效数字
  2. double 8字节 15-16位有效数字

1.2.1 单精度、双精度

#include <iostream>
using namespace std;

int main()
{
    //1单精度  float
    //2双精度  double
    //默认情况下无论单精度还是双精度都只会显示6位有效数字
    float f1 = 3.1415926;  //默认为双精度前面加float相当于把双精度转换为单精度
    float f2 = 3.1415926f;  //后面加一个f直接创建的是单精度

    cout << "f1 = " << f1 << endl;

    double d1 = 3.1415926;

    cout << "d1 = " << d1 << endl;

    cout << "float  占用内存空间为" << sizeof(float) << endl;
    cout << "double 占用内层空间为" << sizeof(double) << endl;

    system("pause");

    return 0;

}

运行结果:

  • f1 = 3.14159
  • d1 = 3.14159
  • float 占用内存空间为4
  • double 占用内层空间为8
  • 请按任意键继续. . .

1.2.2 科学计数法

#include <iostream>
using namespace std;

int main()
{
    //科学计数法
    float f3 = 3e2;  // 3 * (10 ^ 2)
    cout << "f3 = " << f3 << endl;

    float f4 = 3e-2; // 3 * (0.1 ^ 2)
    cout << "f4 = " << f4 << endl;

    system("pause");

    return 0;
}

运行结果:

  • f3 = 300
  • f4 = 0.03
  • 请按任意键继续. . .

1.3 字符型

① 字符型变量用于显示单个字符。

② 语法char ch = 'a';

③ 在显示字符型变量时,用单引号字符括起来,不要用双引号。

④ 单引号内只能有一个字符,不可以是字符串。

⑤ C和C++中字符型变量只占用1个字节。

⑥ 字符型变量并不是把字符本身放到内存中存储而是将对应的ASCII编码放入到存储单元。

⑦ ASCII码大致由以下两部分组成

  1. ASCII非打印控制字符ASCII表上的数字0-31分配给了控制字符用于控制像打印机等一些外围设备。
  2. ASCII打印字符数字21-126分配给了能在键盘上找到的字符当查看或打印文档时就会出现。
#include <iostream>
using namespace std;

int main()
{

    //1字符型变量创建方式
    char ch = 'a';
    cout << ch << endl;

    //2字符型变量所占内存大小
    cout << "char字符型变量所占内存" << sizeof(char) << endl;

    //3字符型变量常见错误
    char ch2 = 'b';  //创建字符型变量时候要用单引号
    //char ch3 = "b";  //错误创建字符型变量时候不能用双引号
    //char ch4 = 'abcdef'; //错误创建字符型变量时候单引号内只能有一个字符

    //4字符型变量对应ASCII编码
    cout << (int)ch << endl;   // (int)ch 为字符'a'的ASCIII码值

    system("pause");

    return 0;

}

运行结果:

  • a
  • char字符型变量所占内存1
  • 97
  • 请按任意键继续. . .

1.4 字符串型

① 用于表示一串字符。

② 字符串型有两种风格

③ C风格字符串char 变量名[] = "字符串值"

④ C++风格字符串string 变量名 = "字符串值"

#include <iostream>
using namespace std;
#include <string> //用C++风格字符串时候,要包含这个头文件

int main()
{

    //1C风格字符串
    //char 字符   =    这是一个字符
    //char 字符[] =    这是一个字符串
    //表示字符串时等号后面要用双引号

    char str[] = "hello world";
    cout << str << endl;

    //2C++风格字符串
    string str2 = "hello world";
    cout << str2 << endl;
    
    system("pause");

    return 0;

}

运行结果:

  • hello world
  • hello world
  • 请按任意键继续. . .

1.5 布尔类型 bool

① 布尔数据类型代表真或假的值。

② bool类型只有两个值

  1. true -- 真 (本质是1)
  2. false -- 假(本质是0)

③ bool类型占1个字节大小

#include <iostream>
using namespace std;

int main()
{

    //1创建bool数据类型

    bool flag1 = true; //true代表真
    cout << flag1 << endl;
    
    bool flag2 = false; //flag 代表假
    cout << flag2 << endl;
    
    //本质上 1代表真 0代表假

    //2查看bool类型所占空间
    cout << "bool所占内存空间" << sizeof(flag1) << endl;
    cout << "bool所占内存空间" << sizeof(flag2) << endl;

    system("pause");

    return 0;

}

运行结果:

  • 1
  • 0
  • bool所占内存空间1
  • bool所占内存空间1
  • 请按任意键继续. . .