3.1 简单变量
3.1.1 变量命名规则
-
只能使用字母、数字和下划线
_ -
第一个字符不能是数字
-
区分大小写(
myVar≠myvar) -
不能使用C++关键字(如
int、double、return等) -
以两个下划线或下划线加大写字母开头的名称保留给编译器使用
// 合法的变量名
int myAge;
double _height;
float score2;
int my_score;
// 非法的变量名
// int 2score; // 不能以数字开头
// double my-val; // 不能含有连字符
// int int; // 不能使用关键字
3.1.2 整型
C++ 提供多种整型,区别在于占用内存大小 和表示范围:
| 类型 | 典型大小 | 范围(有符号) |
|---|---|---|
short |
16位 | -32768 ~ 32767 |
int |
32位 | -2147483648 ~ 2147483647 |
long |
32/64位 | 视平台而定 |
long long |
64位 | -9.2×10¹⁸ ~ 9.2×10¹⁸ |
// limits.cpp -- 查看整型的大小和范围
#include <iostream>
#include <climits> // 包含整型限制常量
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
// sizeof 运算符返回类型或变量的字节数
cout << "int 占 " << sizeof(int) << " 字节" << endl;
cout << "short 占 " << sizeof(short) << " 字节" << endl;
cout << "long 占 " << sizeof(long) << " 字节" << endl;
cout << "long long 占 " << sizeof(long long) << " 字节" << endl;
cout << "\n各类型最大值:" << endl;
cout << "int 最大值:" << n_int << endl;
cout << "short 最大值:" << n_short << endl;
cout << "long 最大值:" << n_long << endl;
cout << "long long 最大值:" << n_llong << endl;
return 0;
}
输出(典型64位系统):
int 占 4 字节
short 占 2 字节
long 占 8 字节
long long 占 8 字节
各类型最大值:
int 最大值:2147483647
short 最大值:32767
long 最大值:9223372036854775807
long long 最大值:9223372036854775807
3.1.3 无符号类型
#include <iostream>
#include <climits>
int main()
{
using namespace std;
unsigned short u_short = USHRT_MAX;
unsigned int u_int = UINT_MAX;
unsigned long u_long = ULONG_MAX;
cout << "unsigned short 最大值:" << u_short << endl;
cout << "unsigned int 最大值:" << u_int << endl;
cout << "unsigned long 最大值:" << u_long << endl;
// 整型溢出演示
short s = SHRT_MAX; // 32767
cout << "\nshort 最大值:" << s << endl;
s++; // 溢出!
cout << "加1后溢出变为:" << s << endl; // -32768
unsigned short us = 0;
cout << "\nunsigned short 最小值:" << us << endl;
us--; // 下溢!
cout << "减1后下溢变为:" << us << endl; // 65535
return 0;
}
输出:
unsigned short 最大值:65535
unsigned int 最大值:4294967295
unsigned long 最大值:18446744073709551615
short 最大值:32767
加1后溢出变为:-32768
unsigned short 最小值:0
减1后下溢变为:65535
⚠️ 整型溢出:超出范围后会"环绕",有符号类型溢出是未定义行为,无符号类型溢出是模运算。
3.1.4 整型字面量(进制表示)
#include <iostream>
int main()
{
using namespace std;
int decimal = 42; // 十进制(默认)
int octal = 042; // 八进制(以0开头)
int hex = 0x42; // 十六进制(以0x开头)
cout << "十进制 42 = " << decimal << endl;
cout << "八进制 042 = " << octal << endl; // 输出34
cout << "十六进制 0x42 = " << hex << endl; // 输出66
// 控制输出格式
cout << "\n以不同进制输出同一个数 255:" << endl;
cout << dec << 255 << endl; // 十进制:255
cout << oct << 255 << endl; // 八进制:377
cout << hex << 255 << endl; // 十六进制:ff
return 0;
}
3.1.5 char 类型
char 类型专门用于存储字符,本质上是一个小整数:
// chartype.cpp -- char类型使用示例
#include <iostream>
int main()
{
using namespace std;
char ch = 'M'; // 用单引号表示字符字面量
cout << "字符:" << ch << endl;
cout << "ASCII码:" << (int)ch << endl; // 强制转换为int查看ASCII值
// char 可以参与算术运算
char next = ch + 1;
cout << "下一个字符:" << next << endl; // N
// 转义字符
cout << "换行:\n";
cout << "制表符:\t对齐" << endl;
cout << "反斜杠:\\" << endl;
cout << "单引号:\'" << endl;
cout << "双引号:\"" << endl;
return 0;
}
常用转义字符:
| 转义字符 | 含义 |
|---|---|
\n |
换行 |
\t |
水平制表符 |
\\ |
反斜杠 |
\' |
单引号 |
\" |
双引号 |
\0 |
空字符(null) |
\a |
响铃 |
\b |
退格 |
3.1.6 signed char 与 unsigned char
#include <iostream>
int main()
{
using namespace std;
// char 的符号性由实现决定
signed char sc = -10;
unsigned char uc = 200;
cout << "signed char: " << (int)sc << endl; // -10
cout << "unsigned char: " << (int)uc << endl; // 200
return 0;
}
3.1.7 wchar_t、char16_t、char32_t(宽字符类型)
#include <iostream>
int main()
{
using namespace std;
wchar_t w = L'A'; // 宽字符,前缀 L
char16_t c16 = u'A'; // UTF-16,前缀 u(C++11)
char32_t c32 = U'A'; // UTF-32,前缀 U(C++11)
wcout << L"宽字符:" << w << endl;
cout << "char16_t 大小:" << sizeof(char16_t) << " 字节" << endl;
cout << "char32_t 大小:" << sizeof(char32_t) << " 字节" << endl;
return 0;
}
3.1.8 bool 类型
// booltype.cpp -- bool类型示例
#include <iostream>
int main()
{
using namespace std;
bool isReady = true;
bool isDone = false;
cout << "isReady = " << isReady << endl; // 输出 1
cout << "isDone = " << isDone << endl; // 输出 0
// 任何非零值转换为bool都是true
bool b1 = -100; // true
bool b2 = 0; // false
bool b3 = 3.14; // true
cout << "bool(-100) = " << b1 << endl; // 1
cout << "bool(0) = " << b2 << endl; // 0
cout << "bool(3.14) = " << b3 << endl; // 1
// 用boolalpha输出true/false字符串
cout << boolalpha;
cout << "isReady = " << isReady << endl; // true
cout << "isDone = " << isDone << endl; // false
return 0;
}
3.2 const 限定符
// consttype.cpp -- const常量示例
#include <iostream>
int main()
{
using namespace std;
// const 定义常量,必须在声明时初始化
const double PI = 3.14159265358979;
const int MONTHS = 12;
const int DAYS_PER_WEEK = 7;
// PI = 3.14; // 错误!不能修改const常量
double radius = 5.0;
double area = PI * radius * radius;
cout << "半径为 " << radius << " 的圆面积:" << area << endl;
cout << "一年有 " << MONTHS << " 个月" << endl;
cout << "一周有 " << DAYS_PER_WEEK << " 天" << endl;
return 0;
}
💡 const vs #define:
const有类型,编译器可以做类型检查
const有作用域限制
#define是简单文本替换,无类型检查推荐使用
const
3.3 浮点数
3.3.1 浮点类型
| 类型 | 大小 | 有效位数 | 范围 |
|---|---|---|---|
float |
32位 | 6~7位 | ±3.4×10³⁸ |
double |
64位 | 15~16位 | ±1.8×10³⁰⁸ |
long double |
80/128位 | 18~19位 | 更大 |
// floatnum.cpp -- 浮点类型示例
#include <iostream>
#include <cfloat> // 浮点数限制常量
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield); // 固定小数点格式
float f = 10.0 / 3.0;
double d = 10.0 / 3.0;
long double ld = 10.0L / 3.0L;
cout << "float 精度:" << f << endl;
cout << "double 精度:" << d << endl;
cout << "long double 精度:" << ld << endl;
cout << "\nfloat 最大值:" << FLT_MAX << endl;
cout << "double 最大值:" << DBL_MAX << endl;
return 0;
}
输出:
float 精度:3.333333
double 精度:3.333333333333333
long double 精度:3.333333333333333481
float 最大值:3.40282e+38
double 最大值:1.79769e+308
3.3.2 浮点字面量
#include <iostream>
int main()
{
using namespace std;
float f1 = 1.5f; // f 或 F 后缀表示 float
float f2 = 1.5e2f; // 科学计数法:150.0
double d1 = 1.5; // 默认是 double
double d2 = 1.5e-3; // 0.0015
long double ld = 1.5L; // L 后缀表示 long double
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
cout << "d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl;
return 0;
}
3.3.3 浮点数的精度问题
#include <iostream>
#include <iomanip>
int main()
{
using namespace std;
float a = 2.34E+22f;
float b = a + 1.0f;
cout << fixed << setprecision(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "b - a = " << b - a << endl; // 结果不是1!
// double 精度更高
double da = 2.34E+22;
double db = da + 1.0;
cout << "\ndouble b - a = " << db - da << endl;
return 0;
}
⚠️ 浮点数精度陷阱 :大数加小数时,小数可能被"吃掉"。需要高精度时使用
double。
3.4 C++ 算术运算符
3.4.1 基本算术运算符
| 运算符 | 含义 | 示例 |
|---|---|---|
+ |
加法 | 5 + 3 = 8 |
- |
减法 | 5 - 3 = 2 |
* |
乘法 | 5 * 3 = 15 |
/ |
除法 | 5 / 3 = 1(整数除法) |
% |
取模(求余) | 5 % 3 = 2 |
// arith.cpp -- 算术运算符示例
#include <iostream>
int main()
{
using namespace std;
int a = 17, b = 5;
cout << "a = " << a << ", b = " << b << endl;
cout << "a + b = " << a + b << endl; // 22
cout << "a - b = " << a - b << endl; // 12
cout << "a * b = " << a * b << endl; // 85
cout << "a / b = " << a / b << endl; // 3(整数除法,截断小数)
cout << "a % b = " << a % b << endl; // 2(余数)
// 浮点除法
double da = 17.0, db = 5.0;
cout << "\nda / db = " << da / db << endl; // 3.4
// 混合运算
cout << "17 / 5 = " << 17 / 5 << endl; // 3(整数)
cout << "17.0 / 5 = " << 17.0 / 5 << endl; // 3.4(浮点)
return 0;
}
3.4.2 运算符优先级与结合性
#include <iostream>
int main()
{
using namespace std;
// 优先级:* / % 高于 + -
int result1 = 2 + 3 * 4; // 2 + 12 = 14
int result2 = (2 + 3) * 4; // 5 * 4 = 20
cout << "2 + 3 * 4 = " << result1 << endl; // 14
cout << "(2 + 3) * 4 = " << result2 << endl; // 20
// 同级运算符从左到右结合
int result3 = 10 - 3 - 2; // (10-3)-2 = 5
cout << "10 - 3 - 2 = " << result3 << endl; // 5
// 取模运算
cout << "13 % 4 = " << 13 % 4 << endl; // 1
cout << "-13 % 4 = " << -13 % 4 << endl; // -1(结果符号与被除数相同)
return 0;
}
3.5 类型转换
3.5.1 自动(隐式)类型转换
// typeconv.cpp -- 自动类型转换示例
#include <iostream>
int main()
{
using namespace std;
// 将一种类型赋值给另一种类型时自动转换
int i = 3.99; // double → int,截断为 3
double d = 3; // int → double,变为 3.0
char c = 65; // int → char,变为 'A'
int j = 'A'; // char → int,变为 65
cout << "int i = 3.99 → " << i << endl; // 3
cout << "double d = 3 → " << d << endl; // 3
cout << "char c = 65 → " << c << endl; // A
cout << "int j = 'A' → " << j << endl; // 65
// 算术运算中的类型提升
// 整数提升:short/char 参与运算时自动提升为 int
short s1 = 100, s2 = 200;
// s1 + s2 的结果是 int 类型
cout << "\nshort + short 结果类型大小:" << sizeof(s1 + s2) << " 字节" << endl;
return 0;
}
3.5.2 强制类型转换
// forcetype.cpp -- 强制类型转换示例
#include <iostream>
int main()
{
using namespace std;
double pi = 3.14159;
int a = 7, b = 2;
// C++风格强制转换(推荐)
int pi_int = static_cast<int>(pi);
double result = static_cast<double>(a) / b;
cout << "static_cast<int>(3.14159) = " << pi_int << endl; // 3
cout << "static_cast<double>(7) / 2 = " << result << endl; // 3.5
// C风格强制转换(不推荐,但仍然有效)
int pi_c = (int)pi;
cout << "(int)3.14159 = " << pi_c << endl; // 3
// 实际应用:计算平均分
int scores[] = {85, 92, 78, 96, 88};
int total = 85 + 92 + 78 + 96 + 88;
double average = static_cast<double>(total) / 5;
cout << "\n平均分:" << average << endl; // 87.8
return 0;
}
3.5.3 C++11 列表初始化与类型转换
#include <iostream>
int main()
{
using namespace std;
// 列表初始化(C++11)不允许缩窄转换
int x = {5}; // 合法
// int y = {3.14}; // 错误!double→int 是缩窄转换
double d = 3.14;
// int z = {d}; // 错误!缩窄转换
int z = static_cast<int>(d); // 必须显式转换
cout << "z = " << z << endl;
return 0;
}
3.6 综合示例
// datatype_demo.cpp -- 第3章综合示例:单位换算器
#include <iostream>
int main()
{
using namespace std;
const double KM_PER_MILE = 1.60934;
const double KG_PER_POUND = 0.453592;
const double CM_PER_INCH = 2.54;
cout << "===== 单位换算器 =====" << endl;
// 英里 → 千米
double miles;
cout << "请输入英里数:";
cin >> miles;
double km = miles * KM_PER_MILE;
cout << miles << " 英里 = " << km << " 千米" << endl;
// 磅 → 千克
double pounds;
cout << "\n请输入磅数:";
cin >> pounds;
double kg = pounds * KG_PER_POUND;
cout << pounds << " 磅 = " << kg << " 千克" << endl;
// 英寸 → 厘米
double inches;
cout << "\n请输入英寸数:";
cin >> inches;
double cm = inches * CM_PER_INCH;
cout << inches << " 英寸 = " << cm << " 厘米" << endl;
// 整数运算示例:计算天数
int total_days;
cout << "\n请输入总天数:";
cin >> total_days;
int years = total_days / 365;
int remain = total_days % 365;
int months = remain / 30;
int days = remain % 30;
cout << total_days << " 天 = "
<< years << " 年 "
<< months << " 个月 "
<< days << " 天" << endl;
return 0;
}
运行示例:
===== 单位换算器 =====
请输入英里数:10
10 英里 = 16.0934 千米
请输入磅数:150
150 磅 = 68.0388 千克
请输入英寸数:72
72 英寸 = 182.88 厘米
请输入总天数:1000
1000 天 = 2 年 8 个月 25 天
📝 第3章知识点总结
| 知识点 | 核心要点 |
|---|---|
| 整型 | short/int/long/long long,有符号与无符号,注意溢出 |
| char | 本质是小整数,单引号表示字符,注意转义字符 |
| bool | true/false,非零即真,输出为1/0 |
| 浮点型 | float(6位)/double(15位)/long double,注意精度问题 |
| const | 定义常量,必须初始化,有类型检查,优于 #define |
| 算术运算符 | + - * / %,整数除法截断,% 只用于整数 |
| 类型转换 | 隐式转换可能丢失精度,推荐用 static_cast<> 显式转换 |
| sizeof | 返回类型或变量占用的字节数 |
| 进制表示 | 0 前缀八进制,0x 前缀十六进制 |