<返回目录     Powered by claude/xia兄

第2课: 数据类型与变量详解

深入理解C++的数据类型系统、变量声明、内存管理和类型转换

基本数据类型

数据类型决定了变量在内存中的存储方式、取值范围和可执行的操作。C++提供了丰富的基本数据类型来满足不同的编程需求。

数据类型分类

基本数据类型表

类型 关键字 大小(字节) 取值范围 说明
布尔型 bool 1 true 或 false 逻辑值,非0即真
字符型 char 1 -128 到 127 或 0 到 255 ASCII字符,有符号或无符号
短整型 short 2 -32,768 到 32,767 节省内存的小整数
整型 int 4 -2,147,483,648 到 2,147,483,647 最常用的整数类型
长整型 long 4/8 取决于系统 32位系统4字节,64位系统8字节
长长整型 long long 8 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 C++11新增,大整数
浮点型 float 4 约±3.4e±38(7位有效数字) 单精度浮点数
双精度浮点型 double 8 约±1.7e±308(15位有效数字) 双精度浮点数,推荐使用
长双精度浮点型 long double 8/12/16 取决于系统 扩展精度浮点数
内存布局理解: 数据类型的大小决定了其在内存中占用的空间。例如,int类型占用4字节(32位),可以表示2³²个不同的值。

数据类型大小验证程序

#include <iostream>
using namespace std;

int main() {
    cout << "=== C++基本数据类型大小 ===" << endl;
    cout << "bool: " << sizeof(bool) << " 字节" << endl;
    cout << "char: " << sizeof(char) << " 字节" << endl;
    cout << "short: " << sizeof(short) << " 字节" << endl;
    cout << "int: " << sizeof(int) << " 字节" << endl;
    cout << "long: " << sizeof(long) << " 字节" << endl;
    cout << "long long: " << sizeof(long long) << " 字节" << endl;
    cout << "float: " << sizeof(float) << " 字节" << endl;
    cout << "double: " << sizeof(double) << " 字节" << endl;
    cout << "long double: " << sizeof(long double) << " 字节" << endl;
    
    return 0;
}

变量声明和初始化

变量是程序中用于存储数据的命名内存位置。在C++中,变量必须先声明后使用。

变量声明规则

命名约定:

变量声明和初始化示例

#include <iostream>
#include <string>  // 需要包含string头文件
using namespace std;

int main() {
    // 声明变量(未初始化,值不确定)
    int age;
    float height;
    char grade;
    bool isStudent;

    // 初始化变量(赋值)
    age = 20;
    height = 1.75;
    grade = 'A';
    isStudent = true;

    // 声明并初始化(推荐方式)
    int score = 95;
    double pi = 3.14159;
    string name = "张三";

    // C++11统一初始化语法
    int count{100};           // 直接初始化
    double temperature{36.5}; // 避免窄化转换
    
    // 输出变量
    cout << "姓名:" << name << endl;
    cout << "年龄:" << age << endl;
    cout << "身高:" << height << "米" << endl;
    cout << "等级:" << grade << endl;
    cout << "是否学生:" << boolalpha << isStudent << endl;

    return 0;
}

变量作用域

变量的作用域决定了它在程序中的可见范围:

#include <iostream>
using namespace std;

int globalVar = 100;  // 全局变量

void testFunction() {
    int localVar = 50;  // 局部变量
    cout << "局部变量:" << localVar << endl;
    cout << "全局变量:" << globalVar << endl;
}

int main() {
    int mainVar = 10;  // main函数的局部变量
    
    {
        int blockVar = 5;  // 块作用域变量
        cout << "块变量:" << blockVar << endl;
    }
    
    // cout << blockVar << endl;  // 错误!blockVar不可见
    
    testFunction();
    cout << "main变量:" << mainVar << endl;
    
    return 0;
}

常量

常量是在程序运行期间值不能改变的量。

#include <iostream>
using namespace std;

int main() {
    // 使用const关键字
    const double PI = 3.14159;
    const int MAX_SIZE = 100;

    // 使用#define预处理器
    #define DAYS_IN_WEEK 7

    cout << "圆周率:" << PI << endl;
    cout << "最大容量:" << MAX_SIZE << endl;

    // PI = 3.14;  // 错误!不能修改常量

    return 0;
}

类型修饰符

unsigned int positiveNum = 100;  // 只能存储正数
long long bigNumber = 9223372036854775807;  // 很大的数
unsigned char byte = 255;  // 0-255范围

类型转换

#include <iostream>
using namespace std;

int main() {
    // 隐式转换
    int num = 10;
    double result = num / 3;  // 结果为3.0(整数除法)
    cout << "隐式转换:" << result << endl;

    // 显式转换(强制类型转换)
    double precise = (double)num / 3;  // 结果为3.33333
    cout << "显式转换:" << precise << endl;

    // C++风格的类型转换
    double value = static_cast<double>(num) / 3;
    cout << "C++风格转换:" << value << endl;

    return 0;
}

sizeof运算符

#include <iostream>
using namespace std;

int main() {
    cout << "int大小:" << sizeof(int) << "字节" << endl;
    cout << "char大小:" << sizeof(char) << "字节" << endl;
    cout << "float大小:" << sizeof(float) << "字节" << endl;
    cout << "double大小:" << sizeof(double) << "字节" << endl;

    int arr[10];
    cout << "数组大小:" << sizeof(arr) << "字节" << endl;

    return 0;
}

练习题

  1. 声明不同类型的变量并初始化,输出它们的值和大小
  2. 编写程序计算圆的面积和周长(使用常量PI)
  3. 尝试整数除法和浮点数除法的区别
  4. 使用类型转换将华氏温度转换为摄氏温度