From a8ecaae14be8cc20c5176d5f2f508cf2be5799f2 Mon Sep 17 00:00:00 2001 From: HelixCopex Date: Sun, 27 Apr 2025 11:44:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E5=88=B0=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E7=AB=A0=E7=BB=93=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 53 +++++++++++++++++++ .vscode/launch.json | 20 +++++++ Chapter_2/2_1_1_details.c | 36 +++++++++++++ Chapter_2/2_2_fathm_ft.c | 15 ++++++ Chapter_2/2_3_two_func.c | 20 +++++++ Chapter_2/2_4_good.c | 19 +++++++ Chapter_2/2_4_nogood.c | 14 +++++ Chapter_2/2_practice.c | 24 +++++++++ Chapter_3/3_10_escape.c | 19 +++++++ Chapter_3/3_1_platinum.c | 22 ++++++++ Chapter_3/3_3_4_int_overflow.c | 11 ++++ Chapter_3/3_3_bases.c | 10 ++++ Chapter_3/3_4_print2.c | 16 ++++++ Chapter_3/3_5_charcode.c | 11 ++++ Chapter_3/3_6_altnames.c | 26 +++++++++ Chapter_3/3_7_1_floaterr.c | 15 ++++++ Chapter_3/3_7_showf_pt.c | 16 ++++++ Chapter_3/3_8_typesize.c | 20 +++++++ Chapter_3/3_9_badcount.c | 17 ++++++ Chapter_3/README.md | 19 +++++++ Chapter_4/README.md | 8 +++ ...个程序_输入_输出_和逻辑运算.md => README.md | 0 22 files changed, 411 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 Chapter_2/2_1_1_details.c create mode 100644 Chapter_2/2_2_fathm_ft.c create mode 100644 Chapter_2/2_3_two_func.c create mode 100644 Chapter_2/2_4_good.c create mode 100644 Chapter_2/2_4_nogood.c create mode 100644 Chapter_2/2_practice.c create mode 100644 Chapter_3/3_10_escape.c create mode 100644 Chapter_3/3_1_platinum.c create mode 100644 Chapter_3/3_3_4_int_overflow.c create mode 100644 Chapter_3/3_3_bases.c create mode 100644 Chapter_3/3_4_print2.c create mode 100644 Chapter_3/3_5_charcode.c create mode 100644 Chapter_3/3_6_altnames.c create mode 100644 Chapter_3/3_7_1_floaterr.c create mode 100644 Chapter_3/3_7_showf_pt.c create mode 100644 Chapter_3/3_8_typesize.c create mode 100644 Chapter_3/3_9_badcount.c create mode 100644 Chapter_3/README.md create mode 100644 Chapter_4/README.md rename notes/第一个程序_输入_输出_和逻辑运算.md => README.md (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..404f556 --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +# .gitignore for C +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cc7fab5 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(Windows) 启动", + "type": "cppvsdbg", + "request": "launch", + "program": "输入程序名称,例如 ${workspaceFolder}/a.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "console": "externalTerminal" + } + + ] +} \ No newline at end of file diff --git a/Chapter_2/2_1_1_details.c b/Chapter_2/2_1_1_details.c new file mode 100644 index 0000000..a2ddff4 --- /dev/null +++ b/Chapter_2/2_1_1_details.c @@ -0,0 +1,36 @@ +#include //C语言并不默认内置输入/输出包 + +//这里是函数头哦 +int main(void) //void main()是不标准写法 +{ + /* 函数必须全部声明才能使用 + 旧标准:其必须放在函数块顶部 */ + + int doors; //变量名应清楚表达自己的用途 + int dogs; //变量可以使用大小写、数字和下划线 + //名称的第一个字符只能是字母或者下划线 + //如果是数字,会报错,不信试试 + //int 3b1b; + int DOGS; //C语言是区分大小写的 + doors = 5; + dogs = 3; + DOGS = 5; + dogs = DOGS; //赋值语句 + printf("We have %d dogs.\n", dogs); + //函数有实际参数和形式参数 + //除了 %d 以外的是实参,%d 是形参 + + /* 新标准下怎么放声明都无所谓,但是这样有可能 + 让代码可读性变差 */ + + int windows; + windows = 404; + int cats; + cats = 114514; + + return 0;//有返回值的C函数要有这句 + //int 表示 main() 函数返回一个整数 + //程序运行到最后时默认会返回0,所以 + //这句在这个情况下写不写区别不大 + //但是保留这一句是个好习惯 +}//花括号括起来的全部是函数体 \ No newline at end of file diff --git a/Chapter_2/2_2_fathm_ft.c b/Chapter_2/2_2_fathm_ft.c new file mode 100644 index 0000000..e483dee --- /dev/null +++ b/Chapter_2/2_2_fathm_ft.c @@ -0,0 +1,15 @@ +//把 2 音寻转换为英寸 +#include +int main(void) +{ + int feet, fathoms; + + fathoms = 2; + feet = 6 * fathoms; + printf("There are %d feet in %d fathoms!\n", feet, fathoms); + printf("Yes, I said %d feet!\n", 6*fathoms); + //给定的传入值不一定要是个变量 + //所以可以有一个传入时计算的语句 + + return 0; +} \ No newline at end of file diff --git a/Chapter_2/2_3_two_func.c b/Chapter_2/2_3_two_func.c new file mode 100644 index 0000000..1832002 --- /dev/null +++ b/Chapter_2/2_3_two_func.c @@ -0,0 +1,20 @@ +/* 一个文件中包含两个函数 */ +#include + +void butler(void);//函数原型(声明) +//这个是现代写法,早期写法长这样: +//void butler(); +//不推荐上面的写法 +int main(void) +{ + printf("I will summon the butler function.\n"); + butler(); + printf("Yes. Bring me some tea and writeable DVDs.\n"); + + return 0; +} + +void butler(void) //函数定义 +{ + printf("You rang, sir?\n"); +}//这种写法有利于厘清程序逻辑 \ No newline at end of file diff --git a/Chapter_2/2_4_good.c b/Chapter_2/2_4_good.c new file mode 100644 index 0000000..69986e2 --- /dev/null +++ b/Chapter_2/2_4_good.c @@ -0,0 +1,19 @@ +/* 这段程序无错误 */ +#include +int main(void) +{ + int n, n2, n3;//修正语法错误 + n = 5; + n2 = n * n; + n3 = n2 * n;//修正语义错误 + printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);//修正语法错误 + + return 0; +} +/* 附上不能用来做变量名的关键字 +auto break case char const continue default +do double else enum extern float for goto +if inline int long register restrict return +short signed sizeof static struct switch typedef +union unsigned void volatile _Bool _Complex _Generic +_Imaginary _Noreturn _Static_assert _Thread_local*/ \ No newline at end of file diff --git a/Chapter_2/2_4_nogood.c b/Chapter_2/2_4_nogood.c new file mode 100644 index 0000000..bca53ad --- /dev/null +++ b/Chapter_2/2_4_nogood.c @@ -0,0 +1,14 @@ +/* 这段程序有错误 */ +#include +int main(void) +{ + int n, int n2, int n3; + + /* 该程序有多处错误 + n = 5 + n2 = n * n; + n3 = n2 * n2; + printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3) + + return 0; +} \ No newline at end of file diff --git a/Chapter_2/2_practice.c b/Chapter_2/2_practice.c new file mode 100644 index 0000000..e03557a --- /dev/null +++ b/Chapter_2/2_practice.c @@ -0,0 +1,24 @@ +#include +int main(void) +{ + //pr.1 + printf("\nPR_1_________\n"); + + printf("Helix Copex\n"); + printf("Helix\nCopex\n"); + printf("Helix "); + printf("Copex\n"); + + //pr.2 + printf("\nPR_2_________\n"); + + printf("Helix Copex, Harbin.\n"); + + //pr.3 + printf("\nPR_3_________\n"); + + int age = 18; + int lifetime = age*365; + printf("Age:%dy, Lifetime:%dd.", age, lifetime); + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_10_escape.c b/Chapter_3/3_10_escape.c new file mode 100644 index 0000000..363edd0 --- /dev/null +++ b/Chapter_3/3_10_escape.c @@ -0,0 +1,19 @@ +/* 使用转义序列 */ +#include +int main(void) +{ + float salary; + + printf("\aEnter your desired monthly salary:"); + printf(" $_______\b\b\b\b\b\b\b"); + //退格只回退光标,不删除字符 + scanf("%f", &salary); + printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0); + //n新起一行,t添加一个制表位的空大小 + printf("\rGee!\n"); + //r使得光标回到起始处 + + getchar(); + getchar(); + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_1_platinum.c b/Chapter_3/3_1_platinum.c new file mode 100644 index 0000000..18436ab --- /dev/null +++ b/Chapter_3/3_1_platinum.c @@ -0,0 +1,22 @@ +/* platinum.c 计算与自身体重等重的白金价值 */ +#include +int main(void) +{ + float weight; + float value; + + printf("Are you worth your weight in platinum?\n"); + printf("Let's check it out.\n"); + printf("Please enter your weight in pounds: "); + + scanf("%f", &weight); + + value = 1700.0 * weight * 14.5833; + printf("your weight in platinum is worth $%.2f.\n", value); + printf("You are easily worth that! If platinum prices drop, \n"); + printf("eat more to maintain your value.\n"); + getchar(); + + getchar(); + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_3_4_int_overflow.c b/Chapter_3/3_3_4_int_overflow.c new file mode 100644 index 0000000..64884e9 --- /dev/null +++ b/Chapter_3/3_3_4_int_overflow.c @@ -0,0 +1,11 @@ +#include +int main(void) +{ + int i = 2147483647; + unsigned int j = 4294967295; + + printf("%d %d %d\n", i, i+1, i+2);//未定义行为 + printf("%u %u %u\n", j, j+1, j+2);//未定义行为 + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_3_bases.c b/Chapter_3/3_3_bases.c new file mode 100644 index 0000000..cc0cab0 --- /dev/null +++ b/Chapter_3/3_3_bases.c @@ -0,0 +1,10 @@ +#include +int main(void) +{ + int x = 100; + + printf("dec = %d; octal = %o; hex = %x\n", x, x, x); + printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x); + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_4_print2.c b/Chapter_3/3_4_print2.c new file mode 100644 index 0000000..54a701f --- /dev/null +++ b/Chapter_3/3_4_print2.c @@ -0,0 +1,16 @@ +/* 更多 printf() 的特性 */ +#include +int main(void) +{ + unsigned int un = 30000000000; + short end = 200; + long big = 65537; + long long verybig = 12345678908642; + + printf("un = %u and not %d\n",un,un); + printf("end = %hd and %d\n",end,end); + printf("big = %ld and not %hd\n",big,big); + printf("verybig= %lld and not %ld\n",verybig,verybig); + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_5_charcode.c b/Chapter_3/3_5_charcode.c new file mode 100644 index 0000000..e83f062 --- /dev/null +++ b/Chapter_3/3_5_charcode.c @@ -0,0 +1,11 @@ +#include +int main(void) +{ + char ch; + + printf("Please enter a character.\n"); + scanf("%c", &ch); + printf("The code for %c is %d.\n",ch,ch); + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_6_altnames.c b/Chapter_3/3_6_altnames.c new file mode 100644 index 0000000..2b9fb9b --- /dev/null +++ b/Chapter_3/3_6_altnames.c @@ -0,0 +1,26 @@ +/* 本文件主要讲述可移植类型,这些类型引入了两个头文件以 +确保 C 语言类型在各个系统中功能相同,定义了一批新的类型 +比如,如果要求一个数精确有32位: +int32_t +或者要求一个数在可以容纳8位的情况下使用一个最小的类型: +int_least8_t +或者,如果你需要一个数容量尽可能大(C99): +intmax_t / uintmax_t +打印时,它们也提供了一些类似的例子。*/ +#include +#include +int main(void) +{ + int32_t me32; + + me32 = 45933945; + printf("First, assume int32_t is int: "); + printf("me32 = %d\n",me32); + printf("Next, let's not make any assumptions.\n"); + printf("Instead, use a\"macro\" from inttypes.h: "); + printf("me32 = %"PRId32" \n",me32); + // 等价于下面这条语句: + printf("me32 = %""d"" \n",me32); + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_7_1_floaterr.c b/Chapter_3/3_7_1_floaterr.c new file mode 100644 index 0000000..74d23c8 --- /dev/null +++ b/Chapter_3/3_7_1_floaterr.c @@ -0,0 +1,15 @@ +/* 演示浮点数舍入错误 */ +#include +int main(void) +{ + float a,b; + + b = 2.0e20 + 1.0; + a = b - 2.0e20; + printf("%f \n", a); + + return 0; +} + +//如果是当前版本 GCC,通常结果为 4008175468544.000000 +//这个结果表明编译器实现遵循:ISO/IEC/IEEE 60559:2011 \ No newline at end of file diff --git a/Chapter_3/3_7_showf_pt.c b/Chapter_3/3_7_showf_pt.c new file mode 100644 index 0000000..a34f247 --- /dev/null +++ b/Chapter_3/3_7_showf_pt.c @@ -0,0 +1,16 @@ +/* 以两种方式显示 float 类型的值 */ +#include +int main(void) +{ + float aboat = 32000.0; + double abet = 2.14e9; + long double dip = 5.32e-5; + + printf("%f can be written %e\n", aboat, aboat); + // 下面的部分要求 C99 支持 + printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat); + printf("%f can be written %e\n",abet,abet); + printf("%Lf can be written %Le\n",dip,dip); + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_8_typesize.c b/Chapter_3/3_8_typesize.c new file mode 100644 index 0000000..32b0c0f --- /dev/null +++ b/Chapter_3/3_8_typesize.c @@ -0,0 +1,20 @@ +/* 打印当前系统类型大小 */ +#include +int main(void) +{ + //C99为类型大小提供转换说明 + printf("Type int has a size of %zd types.\n", + sizeof(int)); + printf("Type char has a size of %zd types.\n", + sizeof(char)); + printf("Type long has a size of %zd types.\n", + sizeof(long)); + printf("Type long long has a size of %zd types.\n", + sizeof(long long)); + printf("Type double has a size of %zd types.\n", + sizeof(double)); + printf("Type long double has a size of %zd types.\n", + sizeof(long double)); + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/3_9_badcount.c b/Chapter_3/3_9_badcount.c new file mode 100644 index 0000000..7370836 --- /dev/null +++ b/Chapter_3/3_9_badcount.c @@ -0,0 +1,17 @@ +/* 函数原型规定了一个函数最多能传入多少个参数,但是 +对于两个输入输出函数来说,这个规则不管用,因为它们的 +参数数量是通过第一个参数来确定的。下面的程序演示了参 +数错误的情况 */ + +#include +int main(void) +{ + int n = 4, m = 5; + float f = 7.0f, g = 8.0f; + + printf("%d\n",n,m); //参数太多 + printf("%d %d %d\n", n); //参数太少 + printf("%d %d\n", f, g); //值的类型不匹配 + + return 0; +} \ No newline at end of file diff --git a/Chapter_3/README.md b/Chapter_3/README.md new file mode 100644 index 0000000..28f1e3e --- /dev/null +++ b/Chapter_3/README.md @@ -0,0 +1,19 @@ +# 本章介绍的内容 + +## 关键字 + +`int` `short` `long` `unsigned` `char` `float` `double` `_Bool` `_Complex` `_Imaginary` + +## 运算符 + +`sizeof()` + +## 函数 + +`scanf()` + +## 具体内容 + +- 整数类型和浮点数类型的区别 +- 如何书写整型和浮点型常数,以及如何声明变量 +- 如何使用 `printf()` 和 `scanf()` 读写不同类型的值 diff --git a/Chapter_4/README.md b/Chapter_4/README.md new file mode 100644 index 0000000..62c71af --- /dev/null +++ b/Chapter_4/README.md @@ -0,0 +1,8 @@ +# 本章介绍的内容 + +- 函数:`strlen()` +- 关键字: `const` +- 字符串 +- 如何创建、存储字符串 +- 如何使用 `strlen()` 函数获取字符串的长度 +- 用 C 预处理器指令 `#define` 和 ANSIC 的 `const` 修饰符创建符号常量 diff --git a/notes/第一个程序_输入_输出_和逻辑运算.md b/README.md similarity index 100% rename from notes/第一个程序_输入_输出_和逻辑运算.md rename to README.md