编程导航

基础知识

  变量的类型有:

  1. 整数类型:int
  2. 浮点类型(小数类型):double、float
  3. 字符类型:char
  4. 布尔类型:bool
  5. 字符串类型:string
  6. 数组类型:arry
  7. 对象类型:object

C++

查询变量信息

  查询值:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

#define TestNumber 0

int main()
{
int IntC = TestNumber;
cout << "十六进制值:" << std::hex << IntC << endl;
cout << "表达值:" << std::dec << IntC << endl;
}

点击测试代码

  查询地址,采用指针的方法:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

#define TestNumber 0

int main()
{
int IntC = TestNumber;
cout << "地址:" << &IntC << endl;
}

点击测试代码

这种变量会在每次运行程序时随机分配,所以地址会发生变化。

  查询所占内存大小,使用函数 sizeof

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

#define TestNumber 0

int main()
{
int IntC = TestNumber;
cout << "长度:" << sizeof(IntC) << endl;
}

点击测试代码

整型

无符号

1
2
3
4
5
6
7
unsigned int UnsignedIntC = 0;
unsigned short int UnsignedShortIntC = 0;
unsigned short UnsignedShortC = 0;
unsigned long int UnsignedLongIntC = 0;
unsigned long UnsignedLongC = 0;
unsigned long long int UnsignedLongLongIntC = 0;
unsigned long long UnsignedLongLongC = 0;

未说明符号

1
2
3
4
5
6
7
int IntC = 0;
short int ShortIntC = 0;
short ShortC = 0;
long int LongIntC = 0;
long LongC = 0;
long long int LongLongIntC = 0;
long long LongLongC = 0;

有符号

1
2
3
4
5
6
7
signed int SignedIntC = 0;
signed short int SignedShortIntC = 0;
signed short SignedShortC = 0;
signed long int SignedLongIntC = 0;
signed long SignedLongC = 0;
signed long long int SignedLongLongIntC = 0;
signed long long SignedLongLongC = 0;

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
using namespace std;

#define TestNumber -1

int main()
{
//整型
//无符号
unsigned int UnsignedIntC = TestNumber;
unsigned short int UnsignedShortIntC = TestNumber;
unsigned short UnsignedShortC = TestNumber;
unsigned long int UnsignedLongIntC = TestNumber;
unsigned long UnsignedLongC = TestNumber;
unsigned long long int UnsignedLongLongIntC = TestNumber;
unsigned long long UnsignedLongLongC = TestNumber;

//无特殊说明
int IntC = TestNumber;
short int ShortIntC = TestNumber;
short ShortC = TestNumber;
long int LongIntC = TestNumber;
long LongC = TestNumber;
long long int LongLongIntC = TestNumber;
long long LongLongC = TestNumber;

//有符号
signed int SignedIntC = TestNumber;
signed short int SignedShortIntC = TestNumber;
signed short SignedShortC = TestNumber;
signed long int SignedLongIntC = TestNumber;
signed long SignedLongC = TestNumber;
signed long long int SignedLongLongIntC = TestNumber;
signed long long SignedLongLongC = TestNumber;

//变量
cout << "变量" << endl;
cout << "|" << "" << endl;
//整型
cout << "|_" << "整型,取值为:" << TestNumber << endl;
cout << "| |" << "" << endl;
//无符号
cout << "| |_" << "无符号" << endl;
cout << "| | |" << "" << endl;
//整型 unsigned int
cout << "| | |_" << "无符号整型 unsigned int" << endl;
cout << "| | | |_" << "地址:" << &UnsignedIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedIntC << endl;
cout << "| | |" << "" << endl;
//短整型 unsigned short int
cout << "| | |_" << "无符号短整型 unsigned short int" << endl;
cout << "| | | |_" << "地址:" << &UnsignedShortIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedShortIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedShortIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedShortIntC << endl;
cout << "| | |" << "" << endl;
//短型 unsigned short
cout << "| | |_" << "无符号短型 unsigned short" << endl;
cout << "| | | |_" << "地址:" << &UnsignedShortC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedShortC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedShortC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedShortC << endl;
cout << "| | |" << "" << endl;
//长整型 unsigned long int
cout << "| | |_" << "无符号长整型 unsigned long int" << endl;
cout << "| | | |_" << "地址:" << &UnsignedLongIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedLongIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedLongIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedLongIntC << endl;
cout << "| | |" << "" << endl;
//长型 unsigned long
cout << "| | |_" << "无符号长型 unsigned long" << endl;
cout << "| | | |_" << "地址:" << &UnsignedLongC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedLongC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedLongC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedLongC << endl;
cout << "| | |" << "" << endl;
//长长整型 unsigned long long int
cout << "| | |_" << "无符号长长整型 unsigned long long int" << endl;
cout << "| | | |_" << "地址:" << &UnsignedLongLongIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedLongLongIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedLongLongIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedLongLongIntC << endl;
cout << "| | |" << "" << endl;
//长长型 unsigned long long
cout << "| | |_" << "无符号长长型 unsigned long long" << endl;
cout << "| | | |_" << "地址:" << &UnsignedLongLongC << endl;
cout << "| | | |_" << "长度:" << sizeof(UnsignedLongLongC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << UnsignedLongLongC << endl;
cout << "| | | |_" << "表达值:" << std::dec << UnsignedLongLongC << endl;
cout << "| |" << "" << endl;

//无特殊说明
cout << "| |_" << "无特殊说明" << endl;
cout << "| | |" << "" << endl;
//整型 int
cout << "| | |_" << "整型 int" << endl;
cout << "| | | |_" << "地址:" << &IntC << endl;
cout << "| | | |_" << "长度:" << sizeof(IntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << IntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << IntC << endl;
cout << "| | |" << "" << endl;
//短整型 short int
cout << "| | |_" << "短整型 short int" << endl;
cout << "| | | |_" << "地址:" << &ShortIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(ShortIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << ShortIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << ShortIntC << endl;
cout << "| | |" << "" << endl;
//短型 short
cout << "| | |_" << "短型 short" << endl;
cout << "| | | |_" << "地址:" << &ShortC << endl;
cout << "| | | |_" << "长度:" << sizeof(ShortC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << ShortC << endl;
cout << "| | | |_" << "表达值:" << std::dec << ShortC << endl;
cout << "| | |" << "" << endl;
//长整型 long int
cout << "| | |_" << "长整型 long int" << endl;
cout << "| | | |_" << "地址:" << &LongIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(LongIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << LongIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << LongIntC << endl;
cout << "| | |" << "" << endl;
//长型 long
cout << "| | |_" << "长型 long" << endl;
cout << "| | | |_" << "地址:" << &LongC << endl;
cout << "| | | |_" << "长度:" << sizeof(LongC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << LongC << endl;
cout << "| | | |_" << "表达值:" << std::dec << LongC << endl;
cout << "| | |" << "" << endl;
//长长整型 long long int
cout << "| | |_" << "长长整型 long long int" << endl;
cout << "| | | |_" << "地址:" << &LongLongIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(LongLongIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << LongLongIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << LongLongIntC << endl;
cout << "| | |" << "" << endl;
//长长型 long long
cout << "| | |_" << "长长型 long long" << endl;
cout << "| | | |_" << "地址:" << &LongLongC << endl;
cout << "| | | |_" << "长度:" << sizeof(LongLongC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << LongLongC << endl;
cout << "| | | |_" << "表达值:" << std::dec << LongLongC << endl;
cout << "| |" << "" << endl;

//有符号
cout << "| |_" << "有符号" << endl;
cout << "| | |" << "" << endl;
//整型 signed int
cout << "| | |_" << "有符号整型 signed int" << endl;
cout << "| | | |_" << "地址:" << &SignedIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedIntC << endl;
cout << "| | |" << "" << endl;
//短整型 signed short int
cout << "| | |_" << "有符号短整型 signed short int" << endl;
cout << "| | | |_" << "地址:" << &SignedShortIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedShortIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedShortIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedShortIntC << endl;
cout << "| | |" << "" << endl;
//短型 signed short
cout << "| | |_" << "有符号短型 signed short" << endl;
cout << "| | | |_" << "地址:" << &SignedShortC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedShortC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedShortC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedShortC << endl;
cout << "| | |" << "" << endl;
//长整型 signed long int
cout << "| | |_" << "有符号长整型 signed long int" << endl;
cout << "| | | |_" << "地址:" << &SignedLongIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedLongIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedLongIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedLongIntC << endl;
cout << "| | |" << "" << endl;
//长型 signed long
cout << "| | |_" << "有符号长型 signed long" << endl;
cout << "| | | |_" << "地址:" << &SignedLongC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedLongC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedLongC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedLongC << endl;
cout << "| | |" << "" << endl;
//长长整型 signed long long int
cout << "| | |_" << "有符号长长整型 signed long long int" << endl;
cout << "| | | |_" << "地址:" << &SignedLongLongIntC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedLongLongIntC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedLongLongIntC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedLongLongIntC << endl;
cout << "| | |" << "" << endl;
//长长型 signed long long
cout << "| | |_" << "有符号长长型 signed long long" << endl;
cout << "| | | |_" << "地址:" << &SignedLongLongC << endl;
cout << "| | | |_" << "长度:" << sizeof(SignedLongLongC) << endl;
cout << "| | | |_" << "十六进制值:" << std::hex << SignedLongLongC << endl;
cout << "| | | |_" << "表达值:" << std::dec << SignedLongLongC << endl;
}

点击测试代码

总结

  1. 无论有符号或者无符号,都可以初始化位一个十进制的负数,都会用相同的补码存储下来,只是表达值不同而已。
  2. 无特殊说明的都是有符号的整型。
  3. 短型不会超过普通的整型,长型不会短于普通的整型。但是具体长度会因为系统位数发生改变。

规范

  其中,Signed 可以省略,默认就是有符号的。而对于长短整型,int 就可以省略。但是后面就不进行省略(缩写)了,写全拼。因为虽然简化了,但是可读性变差了。总结下来就是以下内容:

无符号

1
2
3
4
unsigned int UnsignedIntC = 0;
unsigned short int UnsignedShortIntC = 0;
unsigned long int UnsignedLongIntC = 0;
unsigned long long int UnsignedLongLongIntC = 0;

有符号

1
2
3
4
signed int SignedIntC = 0;
signed short int SignedShortIntC = 0;
signed long int SignedLongIntC = 0;
signed long long int SignedLongLongIntC = 0;

四个无符号的,四个有符号的。核心是四个:整型、短整型、长整型、长长整型。