diff --git a/day4/d5.cpp b/day4/d5.cpp new file mode 100644 index 0000000..fb41388 --- /dev/null +++ b/day4/d5.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int n; // 全局变量 + +// 定义函数 +void test1() +{ + n = 100; +} + +int main() +{ + test1(); // 调用 test1 函数 + cout << "global n = " << n << endl; + { + int m; + cout << "local m = " << m << endl; + } + // 跳出局部区域 m 变量则不能访问 + // cout << "m = " << m << endl; + return 0; +} diff --git a/day4/d7.cpp b/day4/d7.cpp new file mode 100644 index 0000000..e3b066f --- /dev/null +++ b/day4/d7.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +// 声明变量 n 为 int 类型 +extern int n; +// void 表示无返回数据 +void test2() +{ + // 假如存在一个全局变量 n + n += 20; // n=n+20; += 符合赋值运算符 + cout << "int test2 function n = " << n << endl; +} + +int n = 10; +int main() +{ + test2(); + cout << "int main function n = " << n << endl; + return 0; +}