变量名 变量名1, 变量名2,……, 变量名n;ios
int number, price; // 定义整型变量 number 和 price
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while bool catch class const_cast delete dynamic_cast explicit false friend inline namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual
数据类型能够说明一个变量表示什么样的数据(整数、实数、仍是字符等)。不一样数据类型的变量,占用的存储空间大小不一样。 除了基本数据类型外,C++还容许程序员自定义数据类型。程序员
类型名 | 含义 | 字节数 | 取值范围 |
---|---|---|---|
int | 整型,表示整数 | 4 | -2<sub>31</sub> ~ 2<sub>31</sub>-1 |
long | 长整型,表示整数 | 4 | -2<sub>31</sub> ~ 2<sub>31</sub>-1 |
short | 短整型,表示整数 | 2 | -2<sub>15</sub> ~ 2<sub>15</sub>-1 |
unsigned int | 无符号整型,表示非负整数 | 4 | 0 ~ 2<sub>32</sub>-1 |
unsigned long | 无符号长整型,表示非负整数 | 4 | 0 ~ 2<sub>32</sub>-1 |
unsigned short | 无符号短整型,表示非负整数 | 2 | 0 ~ 2<sub>16</sub>-1 |
long long | 64位整型,表示整数 | 8 | -2<sub>63</sub> ~ 2<sub>63</sub>-1 |
unsigned long long | 无符号64位整型,表示非负整数 | 8 | 0 ~ 2<sub>64</sub>-1 |
float | 单精度实数型,表示实数 | 4 | 3.4 x 10<sub>-38</sub> ~ 3.4 x 10<sub>38</sub> |
double | 双精度实数型,表示实数 | 8 | 1.7 x 10<sub>-308</sub> ~ 1.7 x 10<sub>308</sub> |
char | 字符型,表示字符 | 1 | -128 ~ 127 |
unsigned char | 无符号字符型 | 1 | 0 ~ 255 |
bool | 布尔类型,表示真假 | 通常是1 | true或false |
sizeof(变量名) sizeof(类型名) 可以获得某个变量或某一类型占用的字节数this
#include <cstdio> #include <iostream> int main() { int n1 = 10; doube f; char c; printf("%d, %d, %d, %d", sizeof(n1), sizeof(short), sizeof(double), sizeof(c)); // output: 4, 2, 8, 1 }