From b1c38f25c4218d60308ba0d8d5b5aa748345c358 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 15 Jun 2023 15:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=9A=84=E4=BD=9C=E7=94=A8?= =?UTF-8?q?=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/d5.cpp | 24 ++++++++++++++++++++++++ day4/d7.cpp | 21 +++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 day4/d5.cpp create mode 100644 day4/d7.cpp 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; +}