day7 coding: 变量存储、预处理

This commit is contained in:
flykhan 2023-07-11 19:52:53 +08:00
parent 1ad50f4df7
commit ff10f99477
22 changed files with 302 additions and 0 deletions

15
day7/d1.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
void show()
{
int x = 10; // x 只在 show() 函数中有效
printf("show x = %d\n", x);
}
int main(int argc, char const *argv[])
{
int x = 100;
show(); // show 函数内的变量则会自动释放
printf("main x = %d\n", x); // x 是 main 局部定义的 x
return 0;
}

11
day7/d10/a.c Normal file
View File

@ -0,0 +1,11 @@
#include "a.h"
int sum(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}

4
day7/d10/a.h Normal file
View File

@ -0,0 +1,4 @@
#include <stdio.h>
extern int sum(int, int);
extern int sub(int, int);

9
day7/d10/main.c Normal file
View File

@ -0,0 +1,9 @@
#include "./a.h"
int main(int argc, char const *argv[])
{
int ret = sum(100, 200);
printf("100 + 200 = %d\n", ret);
printf("ret - 60 = %d\n", sub(ret, 60));
return 0;
}

11
day7/d11.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
int main()
{
extern int m;
{
int m;
printf("%d\n", m / 0); // 除 0 编译测试
}
return 0;
}

12
day7/d12.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#define PI 3.14
int main()
{
float radiu;
printf("半径(π=%.2f): ", PI);
scanf("%f", &radiu);
printf("面积: %.2f\n", PI * radiu * radiu);
return 0;
}

10
day7/d13.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
#define SUM(a, b) a + b
int main()
{
int x, y;
scanf("%d,%d", &x, &y);
printf("%d+%d=%d\n", x, y, SUM(x, y));
return 0;
}

8
day7/d14.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#define M(a, b) (a) * (b)
int main(int argc, char const *argv[])
{
printf("%d*%d=%d\n", 2 + 5, 6 - 3, M(2 + 5, 6 - 3));
return 0;
}

16
day7/d15.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#define __DEBUG__
int main()
{
#ifdef __DEBUG__
printf("------debug------\n");
#endif
int a = 10, b = 20;
int ret = a + b;
printf("%d+%d=%d\n", a, b, ret);
return 0;
}

18
day7/d16.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#define __DEBUG__
int main()
{
#ifdef __DEBUG__ // 如果定义了 __DEBUG__ 宏,则执行下面代码
printf("------debug------\n");
#else // 如果没有定义 __DEBUG__ 宏,则执行下面的程序
printf("------v1.0------\n");
#endif
int a = 10, b = 20;
int ret = a + b;
printf("%d+%d=%d\n", a, b, ret);
return 0;
}

19
day7/d17.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#define __DEBUG__
int main()
{
#ifndef __DEBUG__ // 如果没有定义了 __DEBUG__ 宏,则执行下面代码
printf("------v1.0------\n ");
#else // 如果定义了 __DEBUG__ 宏,则执行下面的程序
printf(" ------debug-- ----\n ");
#endif
int a = 10, b = 20;
int ret = a + b;
printf("%d+%d=%d\n", a, b, ret);
return 0;
}

16
day7/d18.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#define DEBUG 0
// #define DEBUG 1
int main(int argc, char const *argv[])
{
int a, b;
scanf("%d,%d", &a, &b);
#if DEBUG
printf("%d*%d=%d\n", a, b, a * b);
#else
printf("%d+%d=%d\n", a, b, a + b);
#endif
return 0;
}

11
day7/d2.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
int x; // 全局变量,未初始化,存在静态全局区中
static int y; // 全局静态变量
int main(int argc, char const *argv[])
{
int x = 10; // x 是 main 函数的局部变量,存在栈中
printf("x = %d\n", x); // 局部变量
return 0;
}

19
day7/d3.c Normal file
View File

@ -0,0 +1,19 @@
// 使用全局变量计算100以内累加和。
#include <stdio.h>
int total = 0; // 全局变量在整个
void add(int n)
{
total += n; // 将局部变量 n 的值累加到 total 全局变量
}
int main(int argc, char const *argv[])
{
for (int i = 1; i <= 100; i++)
add(i);
printf("100 以内的整数和: %d\n", total);
return 0;
}

19
day7/d4.c Normal file
View File

@ -0,0 +1,19 @@
// 使用全局变量计算100以内累加和。
#include <stdio.h>
void add(int n)
{
static int total = 0; // 只定义一次静态变量
total += n; // 将局部变量 n 的值累加到 total 全局变量
printf("total = %d\n", total);
}
int main(int argc, char const *argv[])
{
for (int i = 1; i <= 100; i++)
add(i);
// printf("100 以内的整数和: %d\n", total);
return 0;
}

21
day7/d5.c Normal file
View File

@ -0,0 +1,21 @@
// 使用全局变量计算100以内累加和。
#include <stdio.h>
int add(int n)
{
static int total = 0; // 只定义一次静态变量
total += n; // 将局部变量 n 的值累加到 total 全局变量
// printf("total = %d\n", total);
return total;
}
int main(int argc, char const *argv[])
{
int sum;
for (int i = 1; i <= 100; i++)
sum = add(i);
printf("100 以内的整数和: %d\n", sum);
return 0;
}

19
day7/d6.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
static int totalScore;
int totalPersons;
void count(int a, int b, int c)
{
totalScore += a + b + c;
totalPersons++;
}
int main()
{
printf("全局变量值: %d, %d\n", totalPersons, totalScore);
count(90, 100, 89);
count(95, 90, 79);
printf("平均成绩: %.2f\n", totalScore * 1.0 / totalPersons);
return 0;
}

12
day7/d7.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
void sum(int a, int b)
{
int ret = a + b;
printf("%d + %d = %d\n", a, b, ret);
}
int main(int argc, char const *argv[])
{
return 0;
}

19
day7/d8.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
void watch_n(int i)
{
// 监听 watch_n 函数每调 10 次,打印 i 的结果
static int cnt;
if (++cnt % 10 == 0)
{
printf("%d ", i);
}
}
int main(int argc, char const *argv[])
{
for (int i = 0; i < 100; i++)
watch_n(i);
return 0;
}

13
day7/d9/help.c Normal file
View File

@ -0,0 +1,13 @@
#include "help.h"
// 内部函数
static void _show_sum(int a, int b, int c)
{
printf("%d + %d = %d\n", a, b, c);
}
// 全局函数
void twoNumAdd(int a, int b)
{
_show_sum(a, b, a + b);
}

7
day7/d9/help.h Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
// 内部函数
static void _show_sum(int a, int b, int c);
// 全局函数
void twoNumAdd(int a, int b);

13
day7/d9/main.c Normal file
View File

@ -0,0 +1,13 @@
#include "help.h"
int main()
{
for (int i = 1; i < 3; i++)
{
for (int j = 1; j <= i; j++)
{
twoNumAdd(i, j); // 可以调用 help.c 的外部函数
// _show_sum(i, j, i + j); // 不能调用 static 修饰的内部函数
}
}
}