一:标识符
- 变量需要一个名字,称为标识符
- 标识符,一般是由字母,数字,字符组成,一般标识符首位是字母,也可以是下划线 _ ,但绝对不可以是数字
# age 就是变量的标识符 int age = 18;
- 关键字--即从c++保留的标识符,不能用于作为变量的名字
alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq
二:基本数据类型
- 基本数据类型分为算数类型和复合数据类型
其中基本数据类型有:
1. 字符类型,2.整数类型(无符号与有符号),3.浮点类型 , 4.布尔类型 , 5.空类型, 6.空指针
- 其中只有字符类型的char类型是明确的1字节(8位),其余的数据类型位数只有下限,具体多少位要看操作系统和编译器(可移植性更强),具体看下图:
Group | Type names* | Notes on size / precision |
---|---|---|
Character types | char | Exactly one byte in size. At least 8 bits. |
char16_t | Not smaller than char . At least 16 bits. | |
char32_t | Not smaller than char16_t . At least 32 bits. | |
wchar_t | Can represent the largest supported character set. | |
Integer types (signed) | signed char | Same size as char . At least 8 bits. |
signed short int | Not smaller than char . At least 16 bits. | |
signed int | Not smaller than short . At least 16 bits. | |
signed long int | Not smaller than int . At least 32 bits. | |
signed long long int | Not smaller than long . At least 64 bits. | |
Integer types (unsigned) | unsigned char | (same size as their signed counterparts) |
unsigned short int | ||
unsigned int | ||
unsigned long int | ||
unsigned long long int | ||
Floating-point types | float | |
double | Precision not less than float | |
long double | Precision not less than double | |
Boolean type | bool | |
Void type | void | no storage |
Null pointer | decltype(nullptr) |
三:初始化
- 初始化即在声明变量时就给变量一个特定的值
c++ 初始化有三种方式:
1.像c语言一样的初始化(c-like initialization)
int age = 18; // cpp语言要求:标识符age前要指定数据类型;因为继承自c语言
2.构造初始化(constructor initialization)
int age(18); // cpp 语言引入的初始化方式
3.统一初始化(uniform initialization)
int age {18}; // C++ standard 2011 引入的一种初始化方式