diff --git a/Chapter1/Section2/hello.c b/Chapter1/gcc与vim的使用/hello.c similarity index 100% rename from Chapter1/Section2/hello.c rename to Chapter1/gcc与vim的使用/hello.c diff --git a/Chapter1/Section3/a.c b/Chapter1/基本概念-编程注意问题/a.c similarity index 100% rename from Chapter1/Section3/a.c rename to Chapter1/基本概念-编程注意问题/a.c diff --git a/Chapter1/Section3/hello.c b/Chapter1/基本概念-编程注意问题/hello.c similarity index 100% rename from Chapter1/Section3/hello.c rename to Chapter1/基本概念-编程注意问题/hello.c diff --git a/Chapter1/Section3/return.c b/Chapter1/基本概念-编程注意问题/return.c similarity index 100% rename from Chapter1/Section3/return.c rename to Chapter1/基本概念-编程注意问题/return.c diff --git a/Chapter2/变量/auto.c b/Chapter2/变量/auto.c new file mode 100644 index 0000000..2cd70a2 --- /dev/null +++ b/Chapter2/变量/auto.c @@ -0,0 +1,110 @@ +#include +#include + +#if 0 +void func(void) +{ + int x = 0; + x = x + 1; + printf("%p->%d\n", &x, x); +} + +void func1(void) +{ + static int x = 0; + x = x + 1; + printf("%p->%d\n", &x, x); +} + +int main() +{ + + // auto int i; + // printf("i=%d\n", i); + // out:i=21915,每次都不一样 + + // static int i; + // printf("i=%d\n", i); + // out:i=1 + + // func(); + // func(); + // func(); + // out:: + // 0x7ffc3c4ca4f4->1 + // 0x7ffc3c4ca4f4->1 + // 0x7ffc3c4ca4f4->1 + // 三次地址看着一样 + // 但是是每次函数开始取用,结束销毁的 + // 只是gcc刚好都取的栈上同一块地址 + + func1(); + func1(); + func1(); + // out:: + // 0x55fd83c96014->1 + // 0x55fd83c96014->2 + // 0x55fd83c96014->3 + + exit(0); +} +#endif + +#if 0 +int i = 100; + +void func(int i) +{ + printf("i=%d\n", i); +} + +int main() +{ + + int i = 3; + + // printf("i=%d\n", i); + // out: + // i=3 + + // { + // printf("i=%d\n", i); + // } + // out: + // i=3 + + // { + // i = 5; + // printf("i=%d\n", i); + // } + // out: + // i=5 + + // func(i); + + exit(0); +} +#endif + +int i = 0; + +void print_star(void) +{ + for (i = 0; i < 5; i++) + printf("*"); + printf("\n"); + printf("[%s]i=%d\n", __FUNCTION__, i); +} + +int main() +{ + for (i = 0; i < 5; i++) + print_star(); + printf("\n"); + + // out: + // ***** + // [print_star] i = 5 + + exit(0); +} \ No newline at end of file diff --git a/Chapter2/变量/minproj/main.c b/Chapter2/变量/minproj/main.c new file mode 100644 index 0000000..ab83208 --- /dev/null +++ b/Chapter2/变量/minproj/main.c @@ -0,0 +1,15 @@ +#include +#include + +#include "proj.h" + +static int i = 10; + +int main() +{ + printf("[%s]i=%d\n", __FUNCTION__, i); + // func(); + call_func(); + + exit(0); +} \ No newline at end of file diff --git a/Chapter2/变量/minproj/proj.c b/Chapter2/变量/minproj/proj.c new file mode 100644 index 0000000..eed440d --- /dev/null +++ b/Chapter2/变量/minproj/proj.c @@ -0,0 +1,17 @@ +#include +#include + +#include "proj.h" + +static int i = 100; + +static void func(void) +{ + printf("[%s]i=%d\n", __FUNCTION__, i); + exit(0); +} + +void call_func(void) +{ + func(); +} \ No newline at end of file diff --git a/Chapter2/变量/minproj/proj.h b/Chapter2/变量/minproj/proj.h new file mode 100644 index 0000000..d36b958 --- /dev/null +++ b/Chapter2/变量/minproj/proj.h @@ -0,0 +1,5 @@ +#ifndef PROJ_H___ +#define PROJ_H___ + +void call_func(void); +#endif diff --git a/Chapter2/变量/minproj_extern/main.c b/Chapter2/变量/minproj_extern/main.c new file mode 100644 index 0000000..cd335fe --- /dev/null +++ b/Chapter2/变量/minproj_extern/main.c @@ -0,0 +1,14 @@ +#include +#include + +#include "proj.h" + +int i = 10; + +int main() +{ + printf("[%s]i=%d\n", __FUNCTION__, i); + func(); + + exit(0); +} \ No newline at end of file diff --git a/Chapter2/变量/minproj_extern/proj.c b/Chapter2/变量/minproj_extern/proj.c new file mode 100644 index 0000000..eed2237 --- /dev/null +++ b/Chapter2/变量/minproj_extern/proj.c @@ -0,0 +1,13 @@ +#include +#include + +#include "proj.h" + +extern int i; +// extern i;也是可以的,会有警告 + +void func(void) +{ + printf("[%s]i=%d\n", __FUNCTION__, i); + exit(0); +} diff --git a/Chapter2/变量/minproj_extern/proj.h b/Chapter2/变量/minproj_extern/proj.h new file mode 100644 index 0000000..9090088 --- /dev/null +++ b/Chapter2/变量/minproj_extern/proj.h @@ -0,0 +1,5 @@ +#ifndef PROJ_H___ +#define PROJ_H___ + +void func(void); +#endif diff --git a/Chapter2/Section4/define.c b/Chapter2/常量/define.c similarity index 100% rename from Chapter2/Section4/define.c rename to Chapter2/常量/define.c diff --git a/Chapter2/Section2/bool.c b/Chapter2/数据类型/bool.c similarity index 100% rename from Chapter2/Section2/bool.c rename to Chapter2/数据类型/bool.c diff --git a/Chapter2/Section2/float.c b/Chapter2/数据类型/float.c similarity index 100% rename from Chapter2/Section2/float.c rename to Chapter2/数据类型/float.c diff --git a/Chapter2/运算符/op.c b/Chapter2/运算符/op.c new file mode 100644 index 0000000..b78f6d8 --- /dev/null +++ b/Chapter2/运算符/op.c @@ -0,0 +1,103 @@ +#if 0 +i++相当于i=i+1 +i--相当于i=i-1 + +int i=1; +i++;表达式值为1,i值为2 +++i;表达式值为2,i值为2 + +#endif + +#include +#include + +#if 0 +int main() +{ + int i = 1, j = 10, value; + + // value = i++ + ++j; + // 相当于 + // j = j + 1; + // value = i + j; + // i = i + 1; + // out: + // i = 0 j = 11 value = 10 + + // value = --i + j++; + // i = i - 1; + // value = i + j; + // j = j + 1; + // out: + // i = 1 j = 12 value = 12 + + // value = i++ + ++i - i-- + --i; + // 避免单个变量多次自增或者自减 + // 不同编译器可能结果不同,也难为自己和他人 + + // printf("i=%d\n", i); + // printf("j=%d\n", j); + // printf("value=%d\n", value); + + // printf("%d\n", i > j); + + int a = 1, b = 2, c = 3, d = 4; + int m = 1, n = 1; + + // (m = a > b) && (n = c > d); + // printf("m = %d\nn = %d\n", m, n); + // a>b为假,所以左边为0,右边直接不判断了,n依旧为1而不是0! + // out: + // m = 0 + // n = 1 + + (m = a > b) || (n = c > d); + printf("m = %d\nn = %d\n", m, n); + // a>b为假,所以左边为0,右边继续判断 + // out: + // m = 0 + // n = 0 + + exit(0); +} +#endif + +#if 0 +int main() +{ + int i = 0, j = 10, value; + + // int a = 6; + // a -= a *= a += 3; + // a -=.. 81-81=0 + // a *=.. 9*9=81给上面 + // a += 3; 6+3=9给上面 + // 故a=0 + + // printf("%d\n", sizeof(int)); + // printf("%d\n", sizeof(double)); + // out: + // 4 + // 8 + + // int a = 3; + // float f = 3.9; + // a = f; + // printf("a=%d\n", a); + // printf("f=%f\n", f); + // out: + // a=3 + // f=3.900000 + + // int a = 3; + // float f = 3.9; + // a = (int)f; // 这个过程不改变f本身的地址和值 + // printf("a=%d\n", a); + // printf("f=%f\n", f); + // out: + // a=3 + // f=3.900000 + + exit(0); +} +#endif