{"id":15049143,"url":"https://github.com/zzy979/tcpl-code","last_synced_at":"2025-04-10T01:41:29.238Z","repository":{"id":37536204,"uuid":"454870798","full_name":"ZZy979/TCPL-code","owner":"ZZy979","description":"《C程序设计语言》书中代码和练习题解答","archived":false,"fork":false,"pushed_at":"2023-10-07T03:44:21.000Z","size":290,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T03:23:12.754Z","etag":null,"topics":["c","c-programming-language"],"latest_commit_sha":null,"homepage":"https://zzy979.github.io/posts/tcpl-note-index/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZZy979.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-02T17:26:41.000Z","updated_at":"2023-11-05T06:06:40.000Z","dependencies_parsed_at":"2025-02-16T08:45:28.657Z","dependency_job_id":null,"html_url":"https://github.com/ZZy979/TCPL-code","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZZy979%2FTCPL-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZZy979%2FTCPL-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZZy979%2FTCPL-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZZy979%2FTCPL-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZZy979","download_url":"https://codeload.github.com/ZZy979/TCPL-code/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248142128,"owners_count":21054596,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c-programming-language"],"created_at":"2024-09-24T21:18:26.922Z","updated_at":"2025-04-10T01:41:29.206Z","avatar_url":"https://github.com/ZZy979.png","language":"C","readme":"# TCPL-code\n《C程序设计语言》(The C Programming Language)（第2版）书中代码和练习题解答\n\n图书链接：\u003chttps://book.douban.com/subject/1139336/\u003e\n\n笔记：\u003chttps://zzy979.github.io/posts/tcpl-note-index/\u003e\n\n# 运行方式\n## Windows\n方式一：复制到Visual Studio运行\n\n方式二：在命令行中使用gcc编译，例如：\n```\n\u003e cd ch1\n\u003e gcc -o hello_world.exe -ansi hello_world.c\n\u003e hello_world.exe\n```\n\n## Linux\n方式一：使用gcc编译，例如：\n```bash\n$ cd ch1\n$ gcc -o hello_world.out -ansi hello_world.c\n$ ./hello_world.out\n```\n\n方式二：使用make构建\n\n构建单个目标\n```bash\n$ cd ch1\n$ make hello_world.out\n$ ./hello_world.out\n```\n\n构建所有目标\n```bash\n$ make\n```\n既可以在根目录下运行，也可以在每章目录下运行\n\n删除输出文件\n```bash\n$ make clean\n```\n\n# 单元测试\n每章目录下的testdata/tests.txt文件配置要运行的单元测试，每行的格式为\n```\ntarget [output_file] [input_file] [args]\n```\n\n例如：\n```\nfoo_test.out testdata/foo_output.txt testdata/foo_input.txt -x -y\n```\n其中，如果指定了`output_file`，则通过比较程序的标准输出和指定的文件内容进行测试，否则仅判断程序的返回码是否为0。\n\n单元测试运行方式\n```bash\n$ make test\n```\n既可以在根目录下运行，也可以在每章目录下运行\n\n# 代码目录\n## 第1章 导言\n### 1.1 入门\n* [Hello world](ch1/hello_world.c)\n\n### 1.2 变量与算术表达式\n* [打印华氏温度与摄氏温度对照表](ch1/fahrenheit_celsius_table.c)\n* [打印华氏温度与摄氏温度对照表（浮点数版本）](ch1/fahrenheit_celsius_table_float.c)\n* [练习1-3](ch1/exec1-3.c)\n* [练习1-4](ch1/exec1-4.c)\n\n### 1.3 for语句\n* [打印华氏温度与摄氏温度对照表（for循环）](ch1/fahrenheit_celsius_table_for.c)\n* [练习1-5](ch1/exec1-5.c)\n\n### 1.4 符号常量\n* [打印华氏温度与摄氏温度对照表（符号常量）](ch1/fahrenheit_celsius_table_define.c)\n\n### 1.5字符输入/输出\n#### 1.5.1 文件复制\n* [将输入复制到输出（版本1）](ch1/file_copying.c)\n* [将输入复制到输出（版本2）](ch1/file_copying_v2.c)\n\n#### 1.5.2 字符计数\n* [统计输入的字符数（版本1）](ch1/character_counting.c)\n* [统计输入的字符数（版本2）](ch1/character_counting_v2.c)\n\n#### 1.5.3 行计数\n* [统计输入中的行数](ch1/line_counting.c)\n* [练习1-8](ch1/exec1-8.c)\n* [练习1-9](ch1/exec1-9.c)\n* [练习1-10](ch1/exec1-10.c)\n\n#### 1.5.4 单词计数\n* [单词计数](ch1/word_counting.c)\n* [练习1-12](ch1/exec1-12.c)\n\n### 1.6 数组\n* [统计字符的出现次数](ch1/character_counting_array.c)\n* [练习1-13](ch1/exec1-13.c)\n* [练习1-14](ch1/exec1-14.c)\n\n### 1.7 函数\n* [测试power函数](ch1/power.c)\n* [练习1-15](ch1/exec1-15.c)\n\n### 1.8 参数——传值调用\n* [测试power函数（版本2）](ch1/power_v2.c)\n\n### 1.9 字符数组\n* [打印最长的输入行](ch1/print_longest_line.c)\n* [练习1-16](ch1/exec1-16.c)\n* [练习1-17](ch1/exec1-17.c)\n* [练习1-18](ch1/exec1-18.c)\n* [练习1-19](ch1/exec1-19.c)\n\n### 1.10 外部变量与作用域\n* [打印最长的输入行（外部变量）](ch1/print_longest_line_extern.c)\n* [练习1-20](ch1/exec1-20.c)\n* [练习1-21](ch1/exec1-21.c)\n* [练习1-22](ch1/exec1-22.c)\n* [练习1-23](ch1/exec1-23.c)\n\n## 第2章 类型、运算符与表达式\n### 2.2 数据类型及长度\n* [练习2-1](ch2/exec2-1.c)\n\n### 2.3 常量\n* [strlen函数](ch2/strlen.c)\n\n### 2.7 类型转换\n* [atoi函数](ch2/atoi.c)\n* [lower函数](ch2/lower.c)\n* [伪随机数生成器](ch2/rand.c)\n* [练习2-3](ch2/htoi.c)\n\n### 2.8 自增运算符与自减运算符\n* [squeeze函数](ch2/squeeze.c)\n* [strcat函数](ch2/strcat.c)\n* [练习2-4](ch2/squeeze2.c)\n* [练习2-5](ch2/any.c)\n\n### 2.9 按位运算符\n* [getbits函数](ch2/getbits.c)\n* [练习2-6](ch2/setbits.c)\n* [练习2-7](ch2/invert.c)\n* [练习2-8](ch2/rightrot.c)\n\n### 2.10 赋值运算符与表达式\n* [bitcount函数](ch2/bitcount.c)\n* [练习2-9](ch2/bitcount2.c)\n\n### 2.11 条件表达式\n* [练习2-10](ch2/lower2.c)\n\n## 第3章 控制流\n### 3.3 else-if语句\n* [binsearch函数](ch3/binsearch.c)\n* [练习3-1](ch3/exec3-1.c)\n\n### 3.4 switch语句\n* [统计字符的出现次数](ch3/character_counting_switch.c)\n* [练习3-2](ch3/escape.c)\n\n### 3.5 while循环与for循环\n* [atoi函数](ch3/atoi.c)\n* [shellsort函数](ch3/shellsort.c)\n* [reverse函数](ch3/reverse.c)\n* [练习3-3](ch3/expand.c)\n\n### 3.6 do-while循环\n* [itoa函数](ch3/itoa.c)\n* [练习3-4](ch3/itoa2.c)\n* [练习3-5](ch3/itob.c)\n* [练习3-6](ch3/itoa3.c)\n\n### 3.7 break语句与continue语句\n* [trim函数](ch3/trim.c)\n\n## 第4章 函数与程序结构\n### 4.1 函数的基本知识\n* [打印所有与模式匹配的行](ch4/grep.c)\n* [练习4-1](ch4/strrindex.c)\n\n### 4.2 返回非整型值的函数\n* [atof函数](ch4/atof.c)\n* [简单计算器](ch4/rudimentary_calculator.c)\n* [练习4-2](ch4/atof2.c)\n\n### 4.3 外部变量\n* [逆波兰计算器](ch4/reverse_polish_calculator)\n    * [main函数](ch4/reverse_polish_calculator/main.c)\n    * [push和pop函数](ch4/reverse_polish_calculator/stack.c)\n    * [getop函数](ch4/reverse_polish_calculator/getop.c)\n    * [getch和ungetch函数](ch4/reverse_polish_calculator/getch.c)\n* [练习4-3~4-6](ch4/exec4-3~4-6)：扩充逆波兰计算器\n* [练习4-7](ch4/ungets.c)\n* [练习4-8](ch4/getch2.c)\n* [练习4-9](ch4/getch3.c)\n* [练习4-10](ch4/exec4-10/getop.c)\n\n### 4.10 递归\n* [printd函数](ch4/printd.c)\n* [qsort函数](ch4/qsort.c)\n* [练习4-12](ch4/itoa.c)\n* [练习4-13](ch4/reverse.c)\n\n### 4.11 C预处理器\n* [练习4-14](ch4/swap_macro.h)\n\n## 第5章 指针与数组\n### 5.2 指针与函数参数\n* [getint函数](ch5/getint.c)\n* [练习5-1](ch5/getint2.c)\n* [练习5-2](ch5/getfloat.c)\n\n### 5.3 指针与数组\n* [strlen函数](ch5/strlen.c)\n\n### 5.4 地址算术运算\n* [简单存储分配程序](ch5/alloc.c)\n* [strlen函数（版本2）](ch5/strlen2.c)\n\n### 5.5 字符指针与函数\n* [strcpy函数（数组下标版本）](ch5/strcpy.c)\n* [strcpy函数（指针版本1）](ch5/strcpy2.c)\n* [strcpy函数（指针版本2）](ch5/strcpy3.c)\n* [strcpy函数（指针版本3）](ch5/strcpy4.c)\n* [strcmp函数（数组下标版本）](ch5/strcmp.c)\n* [strcmp函数（指针版本）](ch5/strcmp2.c)\n* [练习5-3](ch5/strcat.c)\n* [练习5-4](ch5/strend.c)\n* 练习5-5\n    * [strncpy函数](ch5/strncpy.c)\n    * [strncat函数](ch5/strncat.c)\n    * [strncmp函数](ch5/strncmp.c)\n\n### 5.6 指针数组以及指向指针的指针\n* [排序文本行](ch5/sort)\n    * [main函数](ch5/sort/main.c)\n    * [readlines函数](ch5/sort/readlines.c)\n    * [writelines函数](ch5/sort/writelines.c)\n    * [qsort函数](ch5/sort/qsort.c)\n* [练习5-7](ch5/exec5-7)\n\n### 5.7 多维数组\n* [day_of_year和month_day函数](ch5/date_conversion.c)\n* [练习5-8](ch5/date_conversion2.c)\n\n### 5.9 指针与多维数组\n* [month_name函数](ch5/month_name.c)\n* [练习5-9](ch5/date_conversion3.c)\n\n### 5.10 命令行参数\n* [回显命令行参数（版本1）](ch5/echo.c)\n* [回显命令行参数（版本2）](ch5/echo_v2.c)\n* [打印所有与模式匹配的行](ch5/find.c)\n* [打印所有与模式匹配的行（支持选项参数）](ch5/find_v2.c)\n* [练习5-10](ch5/expr.c)\n* [练习5-11~5-12](ch5/exec5-11~5-12)\n* [练习5-13](ch5/tail.c)\n\n### 5.11 指向函数的指针\n* [排序文本行（版本2）](ch5/sort_v2)\n    * [main函数](ch5/sort_v2/main.c)\n    * [qsort函数](ch5/sort_v2/qsort.c)\n    * [numcmp函数](ch5/sort_v2/numcmp.c)\n* [练习5-14~5-15](ch5/sort_v3)\n\n### 5.12 复杂声明\n* [声明转换](ch5/dcl_convert)\n    * [gettoken函数](ch5/dcl_convert/lexer.c)\n    * [dcl和dirdcl函数](ch5/dcl_convert/parser.c)\n    * [dcl程序](ch5/dcl_convert/dcl.c)\n    * [undcl程序](ch5/dcl_convert/undcl.c)\n\n## 第6章 结构\n### 6.2 结构与函数\n* [point结构和操作函数](ch6/point.c)\n* [rect结构和操作函数](ch6/rect.c)\n\n### 6.3 结构数组\n* [统计C语言关键字出现次数](ch6/keyword_counting)\n    * [main函数](ch6/keyword_counting/main.c)\n    * [key结构](ch6/keyword_counting/key.h)\n    * [getword函数](ch6/getword.c)\n    * [binsearch函数](ch6/keyword_counting/binsearch.c)\n* [练习6-1](ch6/exec6-1)\n\n### 6.4 指向结构的指针\n* [统计C语言关键字出现次数（指针版本）](ch6/keyword_counting_v2)\n    * [main函数](ch6/keyword_counting_v2/main.c)\n    * [binsearch函数](ch6/keyword_counting_v2/binsearch.c)\n\n### 6.5 自引用结构\n* [统计单词出现次数](ch6/word_freq_count)\n    * [main函数](ch6/word_freq_count/main.c)\n    * [tnode结构及操作函数](ch6/word_freq_count/tnode.h)\n    * [strdup函数](ch6/strdup.c)\n* [练习6-2](ch6/exec6-2)\n* [练习6-3](ch6/exec6-3)\n* [练习6-4](ch6/exec6-4)\n\n### 6.6 表查找\n* [散列表](ch6/hash_table.c)\n* [练习6-5](ch6/hash_table.c)\n* [练习6-6](ch6/exec6-6)\n\n## 第7章 输入与输出\n### 7.1 标准输入/输出\n* [将输入转换为小写形式](ch7/lower.c)\n* [练习7-1](ch7/exec7-1.c)\n\n### 7.2 格式化输出——printf\n* [练习7-2](ch7/exec7-2.c)\n\n### 7.3 变长参数表\n* [minprintf函数](ch7/minprintf.c)\n* [练习7-3](ch7/minprintf2.c)\n\n### 7.4 格式化输入——scanf\n* [简单计算器](ch7/rudimentary_calculator.c)\n* [练习7-4](ch7/minscanf.c)\n* [练习7-5](ch7/reverse_polish_calculator)\n\n### 7.5 文件访问\n* [拼接文件（版本1）](ch7/cat.c)\n\n### 7.6 错误处理——stderr和exit\n* [拼接文件（版本2）](ch7/cat_v2.c)\n\n### 7.7 行输入和输出\n* [getline函数](ch7/getline.c)\n* [练习7-6](ch7/diff.c)\n* [练习7-7](ch7/find.c)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzzy979%2Ftcpl-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzzy979%2Ftcpl-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzzy979%2Ftcpl-code/lists"}