commit 0baf07a5974a3f0b1ee8cc759cdd3188b5b81ed7 Author: lzy Date: Sun Mar 10 16:11:35 2024 +0800 Initial commit diff --git a/Chapter1/Section2/hello b/Chapter1/Section2/hello new file mode 100755 index 0000000..7486a75 Binary files /dev/null and b/Chapter1/Section2/hello differ diff --git a/Chapter1/Section2/hello.c b/Chapter1/Section2/hello.c new file mode 100644 index 0000000..067aba8 --- /dev/null +++ b/Chapter1/Section2/hello.c @@ -0,0 +1,16 @@ +#include +#include + +#if 0 +void main(void); +void main(int argc, char **argv);// char *argv[] +int main(int argc, char **argv); +int main(void); +#endif + +int main(void) +{ + printf("Hello world!\n"); + + exit(0); +} diff --git a/Chapter1/Section3/a b/Chapter1/Section3/a new file mode 100755 index 0000000..f59ba6c Binary files /dev/null and b/Chapter1/Section3/a differ diff --git a/Chapter1/Section3/a.c b/Chapter1/Section3/a.c new file mode 100644 index 0000000..009cdd2 --- /dev/null +++ b/Chapter1/Section3/a.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +int main() +{ + FILE *fp; + + fp = fopen("tmp", "r"); + if (fp == NULL) + { + fprintf(stderr, "fopen():%s\n", strerror(errno)); + exit(1); + } + puts("ok!"); + exit(0); +} \ No newline at end of file diff --git a/Chapter1/Section3/a.out b/Chapter1/Section3/a.out new file mode 100755 index 0000000..b8a0399 Binary files /dev/null and b/Chapter1/Section3/a.out differ diff --git a/Chapter1/Section3/hello b/Chapter1/Section3/hello new file mode 100755 index 0000000..4af5a31 Binary files /dev/null and b/Chapter1/Section3/hello differ diff --git a/Chapter1/Section3/hello.c b/Chapter1/Section3/hello.c new file mode 100644 index 0000000..e4b8681 --- /dev/null +++ b/Chapter1/Section3/hello.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + int *p = NULL; + int i; + p = (int *)malloc(sizeof(int)); + if (p == NULL) + return -1; + printf("Hello world!\n"); + return 0; +} \ No newline at end of file diff --git a/Chapter1/Section3/return b/Chapter1/Section3/return new file mode 100755 index 0000000..e0dfb5d Binary files /dev/null and b/Chapter1/Section3/return differ diff --git a/Chapter1/Section3/return.c b/Chapter1/Section3/return.c new file mode 100644 index 0000000..43acf2f --- /dev/null +++ b/Chapter1/Section3/return.c @@ -0,0 +1,6 @@ +#include + +int main(void) +{ + printf("Hello world!\n"); +} diff --git a/Chapter2/Section2/bool b/Chapter2/Section2/bool new file mode 100755 index 0000000..3a717a6 Binary files /dev/null and b/Chapter2/Section2/bool differ diff --git a/Chapter2/Section2/bool.c b/Chapter2/Section2/bool.c new file mode 100644 index 0000000..b2b9f01 --- /dev/null +++ b/Chapter2/Section2/bool.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main() +{ + bool a = false; + printf("a = %d\n", a); + exit(0); +} \ No newline at end of file diff --git a/Chapter2/Section2/float.c b/Chapter2/Section2/float.c new file mode 100644 index 0000000..410439e --- /dev/null +++ b/Chapter2/Section2/float.c @@ -0,0 +1,10 @@ +int func(float f) +{ + if (f < 0) + return -1; + // else if (f == 0) + else if (fabs(f - 0) <= 1e-6) + return 0; + else + return 1; +} \ No newline at end of file diff --git a/Chapter2/Section4/define b/Chapter2/Section4/define new file mode 100755 index 0000000..64545b6 Binary files /dev/null and b/Chapter2/Section4/define differ diff --git a/Chapter2/Section4/define.c b/Chapter2/Section4/define.c new file mode 100644 index 0000000..ac39915 --- /dev/null +++ b/Chapter2/Section4/define.c @@ -0,0 +1,60 @@ +#include +#include + +#define PI 3.14 +#define ADD 2 + 3 +#define ADD1 (2 + 3) + +#define MAX(a, b) (a > b ? a : b) +#define MAX1(a, b) ((a) > (b) ? (a) : (b)) + +#define MAX2(a, b) \ + ({ \ + int A = a, B = b; \ + ((A) > (B) ? (A) : (B)); \ + }) + +#define MAX3(a, b) \ + ({ \ + typeof(a) A = a, B = b; \ + ((A) > (B) ? (A) : (B)); \ + }) + +int max(int a, int b) +{ + return a > b ? a : b; +} + +int main() +{ + // printf("%d\n", ADD * ADD); + // 相当于 2+3*2+3 + // 故输出11而不是25 + // printf("%d\n", ADD1 * ADD1); + // 相当于(2+3)*(2+3) + // 正常输出25 + + int i = 5, j = 3; + // printf("%d\n", MAX(i, j)); + // 输出5 + + // printf("%d\n", MAX(i, j * 2)); + // 输出6 + + // printf("i=%d\tj=%d\n", i, j); + // printf("%d\n", MAX1(i++, j++)); + // printf("i=%d\tj=%d\n", i, j); + // 输出: + // i = 5 j = 3 + // 6 + // i = 7 j = 4 + // 为什么i自增了两次? + // 预处理结果 + // printf("%d\n", ((i++) + (j++) ? (i++) : (j++))); + + printf("i=%d\tj=%d\n", i, j); + printf("%d\n", MAX2(i++, j++)); + printf("i=%d\tj=%d\n", i, j); + + exit(0); +} \ No newline at end of file