#include
//系统定义头文件一般是尖括号
#include
#include
using namespace std;
int a = 10; //全局变量
int fun() {
return 0;
};
int main()
{
//基本内置类型 变量 引用与指针 const限定符 自定义数据结构
//语法:法律
//经验:道德约束
//1、全局变量与局部变量重名
int a = 6; //重名,局部变量会屏蔽全局变量;
cout << "a:" << a << endl;
cout << "全局变量a:" << ::a << endl; //虽然这样可以获取全局变量的值,但是不便于理解,因此不要重名。
return 0;
}
#include
//系统定义头文件一般是尖括号
#include
#include
using namespace std;
int a = 10; //全局变量
int fun() {
return 0;
};
int main()
{
//2、静态变量
//这个变量存储在静态存储区中。这个变量的生命周期比较长。只有等到程序执行完成,进行内存空间回收的时候才会消失。
//如果想要每次程序执行的时候,这个变量的值都不变,就使用静态变量来处理。
static int b = 8;
//此外静态变量还可以如下使用:
for (int i = 0; i < 10; i++) {
int d = 1;
d++;
cout << "d:" << d << endl;
}
//静态变量用于循环中:
cout << "\n" << endl;
for (int i = 0; i < 10; i++) {
static int c = 1;
c++;
cout << "c:" << c << endl;
}
return 0;
}
1、一般选int(4个字节,int可以表示的数据范围是-21亿到21亿左右),short (2个字节,16位,2^16次方个数的值表示)太小,int不够选long long(8个字节);
2、不可能为负数,选无符号,无符号有符号不要混用;
3、不要使用char做算术运算;
4、浮点数运算最好选用double,精度高而且性能和float几乎没有差别。
养成变量创建就要初始化的好习惯,特别是指针,初始化为空指针nullptr(0x00000000的地址)。
内置类型有默认初始值,但是不建议依赖这个。
#include
//系统定义头文件一般是尖括号
#include
#include
using namespace std;
int a = 10; //全局变量
int fun() {
return 0;
};
int main()
{
int b = 8;
//5、少用多层指针,最多用到两层(指向指针的指针)
int* p = &b;
//指向指针的指针
int** pp = &p;
cout << "b地址:" << &b << endl;
cout << "p指针:" << p << endl;
cout << "pp指针(指向指针的指针):" << pp << endl;
//写指针尽量简单一些,利于问题排查。
return 0;
}