读书笔记之-----"The C Programming Language"


    最近找工作,发现一个很尴尬的事情,研究生阶段主要做的都是研究方向的问题,应该算是偏向于软件以及算法(其实也就是编程实现一些cv的算法,常用语言matlab),所以就在软件方向找了份软件开发工程师的工作。因为机试用的是C语言,而本科学得C语言都还给老师了,所以紧急学了两天,总算还没忘干净,最终拿到了offer,虽然如此,但是以后还是要用到的,所以还是利用十一重新学习了一下。因为c语言的书太多了,大部分都据说很烂(话说我们学的谭浩强的书,个人感觉就很恐怖,感觉就不是把你往明白的方向教),所以选了一部比较好的书“The C Programming Language”,就是那本霸气的K&R C Bible(话说是印在书的封面上的哦!)。读完后觉得称之为Bible还是应该的,不是因为这本书的作者是C语言的开发者,也不是因为大家都推荐它,而是因为这本书能很条理的讲述C语言的知识,一步一步的教你怎样写能够用的上的函数,在函数的编程中理解各种基本知识与概念,而不是做那种凭空想出来的题目。而且我觉得看英文版的能够比较效率的学习,因为经过了一层的翻译,尤其是很烂的翻译后,总会丢失一些东西,而且很多概念英文是很清晰的,而翻译过来有时真的会莫名其妙,最重要的是,看英文原版的书,能够更加专心,更加用心的去理解,不会那么浮躁(可能是我自己的毛病),因此能够收获更多。

下面是对这本书的一些总结,算是读书笔记吧。

第一章:tutorial introduction

是全书的一个tutorial introduction,基本覆盖了c语言的大部分基本内容,当然指针,结构这些内容还是c语言中比较高阶的东西,所以没有覆盖。首先是Hello World函数,这个就不说了。然后是一个温度转换函数,这个函数基本覆盖了变量定义,循环,表达式,以及格式化输出等内容,这个算是一个比较有意义的完整的c程序了。本章内容还包括了for循环,宏定义,字符输入输出,数组,函数,值传递,字符数组,外部变量与作用域等内容,比较全面而且简单的对这些知识做了一定的介绍,后面的章节会在这个上面展开。

第二章:Types,Operators,and Expressions

本章按照标题来看是分成了3个部分,类型,运算符,表达式。

首先介绍了variable name:数字,字母,下划线(不能以数字开头,也不推荐以下划线开头,因为库文件是以下划线开头的)。还不能用关键字

Data Type:char,int,float,double是最基本的数据类型,当然加上short,long,unsigned,signed修饰符,可以得到更加丰富的类型。

Constants:与变量对应的就是常量,字符常量可以看成是整数。

Type Conversion:这里有一个点就是强制类型转换,(type_name) expression

Bitwise Operators: 能够实现一些逻辑检验的问题。

Precedence:优先级,其实本章最重要的部分应该就是这里了。各种operators都是比较简单好记的,唯有优先级问题是比较难记的,若是每次都查的话太没水平了,也绝对不利于理解后面的函数,这个应该算是基本功。总的顺序是unary>binary.优先级最高的严格意义上说不算operator,这些是括号(),数组索引括号[],结构成员选择符(->,.)。然后是剩下的一元运算符,非,取反,自增,自减,类型转换,指针,取址,sizeof。这一组的连接方式是right to left。然后是2维运算符,算术>移位>关系>逻辑。然后是一个right to left的三维运算符,然后是赋值运算符,最后是逗号运算符。

第三章:Control Flow

一门编程语言的语句有三种:顺序,条件,和循环,这就构成了编程语言的控制流,3种简单的控制语句构成了各种强大的程序,甚至是一些神一样的程序,这确实是一件很amazing的事情。条件语句有if-else,elseif,switch。switch是需要注意的,这个是条件分支结构,常常需要跟break结合在一起发挥作用,一般情况下case后面都要加上break,如果不加的话,会继续执行下一条case,一直到遇到break为止。但是有些程序在需要的情况下不在每一个case后面加上break,这样会产生有益的效果,这也是需要注意的(这种情况下最好注释说明一下)。循环语句有while,do-while,for三种。本章中写的比较有意义的程序有shell sort,bin-search以及alpha-num count。当然还有诡异的goto语句,其实个人觉得goto语句还是很有意义的啊,可以快速跳出循环,跳到下个执行的地方,代价会是搞得程序比较混乱。但是书中作者和各位老师都不提倡用goto,并且不用goto也能写出很好的程序,所以就never mind吧。

第四章:Function and Program Structure

c语言最重要的就是函数,c就是由函数构成的,程序的主体是包含在main{}中的部分,main函数也是函数,有着与其他函数一样的性质,比如说作用域,参数,返回值的问题等等。函数有声明和定义,在用到一个函数之前必须先声明。函数可以声明多次,但是只能定义一次。c语言不允许在函数中定义函数,这和matlab等是不一样的,因为程序都是并列存在的,所以所有的函数都是external。变量也分为外部变量和内部变量。内部变量的作用域是函数本身,外部变量的作用域是程序的整个执行空间。静态变量static是用在外部变量和函数的修饰符,表明这些函数或变量的作用域只是到此源文件的结尾。寄存器变量register,是定义在了计算机的寄存器上的变量,应该说在运行速度上肯定是很有优势的,但是实际意义,还没怎么看得到。递归,是一个很神奇的东西,它能很大程度上简化程序的复杂度,但是缺点是,时间复杂度太大。递归在数据结构上会有很大应用,尤其是tree。递归必须要有一个终止条件,否则就是死循环。最后本章还包含了宏替换的内容,需要注意的是要给每个变量加上括号,如#define max(A,B) ((A)>(B)?(A):(B)),否则后果会很严重。本章写了相当多的有意义的函数,如getline,strindex,atof,出入栈函数(push,pop),getch和ungetch,快速查找quicksort,以及取最大值的宏替换等等。同时也很系统的做了一个计算器的程序,用到了子函数,外部变量,栈等等,算是一个比较完备的程序了。

第五章:Pointers and Arrays

指针才是c语言的精华所在。如果学c语言不学指针或者说不懂指针的话,那就只能呵呵了。从全书来看,pointer绝对是重中之重,函数的形参定义要用到,结构的定义要用到,数据结构也要用到,前面的内容,其实各种编程语言都是差不多的,懂了一种编程语言,那么基本上看看就能掌握,但是这对指针不适用。指针应该是c语言的独创,c语言30年不倒,跟指针应该说是有很大关系的,c与底层硬件的紧密联系应该说指针也是有功劳的。指针是包含变量地址的变量,因此指针也是变量(这一点很重要)。指针的一个比较大的用处应该是在函数的形参声明上,因为c语言的函数是值传递的,因此如果用普通的int等变量定义函数的行参,那么实际上函数运行的时候,只是修改的是输入的变量的副本,对实参是没有任何改变的,因此其实是什么都没有做的,这跟c语言的值传递,以及作用域原则有关。而指针和数组则不存在这样的问题,数组和指针在相当大的程度上是相通的,而且指针比较简洁,所以形参声明会用到指针。指针指向的是数组的元素,p是指针,a[]是数组,那么p=a;指针指向数组时是可以做加减法的,当然要在数组的索引范围内。指针和数组名的区别是指针是变量,而数组名不是,这也就意味着不可以做b=a以及a++这样的事情。指向指针的指针,也就是指针数组,就相当于是数组中存的内容是指针,我觉得这个主要是为了替代多维数组而存在的。而指针数组相对比多维数组的优点是不用为每个子数组定义相同大小的空间。指向函数的指针,函数不是变量,但是可以定义指向函数的指针,我觉得意义就在于可以让程序选择在不同情况下用哪个函数,例如(int (*)(void*,void*))(numeric?numcmp:strcmp),numcmp和strcmp是两个函数。另外本章还提到了命令行参数,args(命令行参数的个数),argv[](指向命令行参数的指针数组)。这一章写了更多的函数swap,strlen,alloc,strcmp,strcpy,getline,writeline,重写的qsort函数等等。

第六章:Structure

我感觉structure就是为了构建数据结构而存在的!Structure是用于构建属于自己的data type,就像int,char一样,实际而言,他们的用法是一样的。因此,自然就会有pointer,function,array等等内容。还有结构的自引用,有点递归的意思,这个在构建tree结构的时候有了很好的应用。Union的用处在于程序的兼容,当定义变量时,我们不确定要定义成什么类型时,也即不知到该分配多少空间时,union可以表现一下。本章的另一个比较重要的点是:typedef。typedef并不能创建一个新的数据类型,他只是把原有的数据类型又重新定义了一个名字,看上去没什么用,但是真的没用么?当然不是,这么做的目的一个是可移植性,因为不同的machine对于int(举个例子)的空间分配是不同的,所以当需要移植的时候,只需要修改typedef就可以了;第二个就是能使变量的意义更明了,比如typedef inttreeptr,当定义int型的树指针时,用treestr更加清晰。本章的程序主要是keyword_count,tree,以及table_lookup,其中tree这个函数系列涉及到了C语言的很多方面,作为一个例子来说,比较有深度。

第七章:Input and Output

本章介绍了一些输入输出的接口函数,getchar,putchar, printf, scanf, sprintf, sscanf, getc, putc, fopen, fclose,stdin, stdout, fputs, fgets. 并且介绍了几个比较有用的头文件:<ctype.h>,<string.h>, <math.h>.

本书还有两个附录:一个是Reference Manual, 另一个是Standard Library. 有时间要翻翻看看。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 ISBN 0-13-110362-8 (paperback) ISBN 0-13-110370-9 目录结构: Contents Preface Preface to the first edition Introduction Chapter 1: A Tutorial Introduction Getting Started Variables and Arithmetic Expressions The for statement Symbolic Constants Character Input and Output File Copying Character Counting Line Counting Word Counting Arrays Functions Arguments - Call by Value Character Arrays External Variables and Scope Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels Chapter 4: Functions and Program Structure Basics of Functions Functions Returning Non-integers External Variables Scope Rules Header Files Static Variables Register Variables Block Structure Initialization Recursion The C Preprocessor File Inclusion Macro Substitution Conditional Inclusion Chapter 5: Pointers and Arrays Pointers and Addresses Pointers and Function Arguments Pointers and Arrays Address Arithmetic Character Pointers and Functions Pointer Arrays; Pointers to Pointers Multi-dimensional Arrays Initialization of Pointer Arrays Pointers vs. Multi-dimensional Arrays Command-line Arguments Pointers to Functions Complicated Declarations Chapter 6: Structures Basics of Structures Structures and Functions Arrays of Structures Pointers to Structures Self-referential Structures Table Lookup Typedef Unions Bit-fields Chapter 7: Input and Output Standard Input and Output Formatted Output - printf Variable-length Argument Lists Formatted Input - Scanf File Access Error Handling - Stderr and Exit Line Input and Output Miscellaneous Functions String Operations Character Class Testing and Conversion Ungetc Command Execution Storage Management Mathematical Functions Random Number generation Chapter 8: The UNIX System Interface File Descriptors Low Level I/O - Read and Write Open, Creat, Close, Unlink Random Access - Lseek Example - An implementation of Fopen and Getc Example - Listing Directories Example - A Storage Allocator Appendix A: Reference Manual Introduction Lexical Conventions Syntax Notation Meaning of Identifiers Objects and Lvalues Conversions Expressions Declarations Statements External Declarations Scope and Linkage Preprocessor Grammar Appendix B: Standard Library Input and Output: <stdio.h> File Operations Formatted Output Formatted Input Character Input and Output Functions Direct Input and Output Functions File Positioning Functions Error Functions Character Class Tests: <ctype.h> String Functions: <string.h> Mathematical Functions: <math.h> Utility Functions: <stdlib.h> Diagnostics: <assert.h> Variable Argument Lists: <stdarg.h> Non-local Jumps: <setjmp.h> Signals: <signal.h> Date and Time Functions: <time.h> Implementation-defined Limits: <limits.h> and <float.h> Appendix C: Summary of Changes
The C programming Language 第二版英文版 內容列表 Table of Contents Preface.......................................................... Preface to the first edition..................................... Introduction..................................................... Chapter 1 - A Tutorial Introduction.............................. 1.1 Getting Started................................ 1.2 Variables and Arithmetic Expressions........... 1.3 The for statement.............................. 1.4 Symbolic Constants............................. 1.5 Character Input and Output..................... 1.5.1 File Copying.......................... 1.5.2 Character Counting.................... 1.5.3 Line Counting......................... 1.5.4 Word Counting......................... 1.6 Arrays......................................... 1.7 Functions...................................... 1.8 Arguments - Call by Value...................... 1.9 Character Arrays............................... 1.10 External Variables and Scope.................. Chapter 2 - Types, Operators and Expressions..................... 2.1 Variable Names................................. 2.2 Data Types and Sizes........................... 2.3 Constants...................................... 2.4 Declarations................................... 2.5 Arithmetic Operators........................... 2.6 Relational and Logical Operators............... 2.7 Type Conversions............................... 2.8 Increment and Decrement Operators.............. 2.9 Bitwise Operators.............................. 2.10 Assignment Operators and Expressions.......... 2.11 Conditional Expressions....................... 2.12 Precedence and Order of Evaluation............ Chapter 3 - Control Flow......................................... 3.1 Statements and Blocks.......................... 3.2 If-Else........................................ 3.3 Else-If........................................ 3.4 Switch......................................... 3.5 Loops - While and For.......................... 3.6 Loops - Do-While............................... 3.7 Break and Continue............................. 3.8 Goto and labels................................ Chapter 4 - Functions and Program Structure...................... 4.1 Basics of Functions............................ 4.2 Functions Returning Non-integers............... 4.3 External Variables............................. 4.4 Scope Rules.................................... 4.5 Header Files................................... 4.6 Static Variables................................ 4.7 Register Variables.............................. 4.8 Block Structure................................. 4.9 Initialization.................................. 4.10 Recursion...................................... 4.11 The C Preprocessor............................. 4.11.1 File Inclusion........................ 4.11.2 Macro Substitution.................... 4.11.3 Conditional Inclusion................. Chapter 5 - Pointers and Arrays.................................. 5.1 Pointers and Addresses......................... 5.2 Pointers and Function Arguments................ 5.3 Pointers and Arrays............................ 5.4 Address Arithmetic............................. 5.5 Character Pointers and Functions............... 5.6 Pointer Arrays; Pointers to Pointers........... 5.7 Multi-dimensional Arrays....................... 5.8 Initialization of Pointer Arrays............... 5.9 Pointers vs. Multi-dimensional Arrays.......... 5.10 Command-line Arguments........................ 5.11 Pointers to Functions......................... 5.12 Complicated Declarations...................... Chapter 6 - Structures........................................... 6.1 Basics of Structures........................... 6.2 Structures and Functions....................... 6.3 Arrays of Structures........................... 6.4 Pointers to Structures......................... 6.5 Self-referential Structures.................... 6.6 Table Lookup................................... 6.7 Typedef........................................ 6.8 Unions......................................... 6.9 Bit-fields..................................... Chapter 7 - Input and Output..................................... 7.1 Standard Input and Output....................... 7.2 Formatted Output - printf....................... 7.3 Variable-length Argument Lists.................. 7.4 Formatted Input - Scanf......................... 7.5 File Access..................................... 7.6 Error Handling - Stderr and Exit................ 7.7 Line Input and Output........................... 7.8 Miscellaneous Functions......................... 7.8.1 String Operations...................... 7.8.2 Character Class Testing and Conversion. 7.8.3 Ungetc................................. 7.8.4 Command Execution...................... 7.8.5 Storage Management..................... 7.8.6 Mathematical Functions................. 7.8.7 Random Number generation............... Chapter 8 - The UNIX System Interface............................ 8.1 File Descriptors............................... 8.2 Low Level I/O - Read and Write................. 8.3 Open, Creat, Close, Unlink..................... 8.4 Random Access - Lseek.......................... 8.5 Example - An implementation of Fopen and Getc.. 8.6 Example - Listing Directories.................. 8.7 Example - A Storage Allocator.................. Appendix A - Reference Manual.................................... A.1 Introduction................................... A.2 Lexical Conventions............................ A.2.1 Tokens................................ A.2.2 Comments.............................. A.2.3 Identifiers........................... A.2.4 Keywords.............................. A.2.5 Constants............................. A.2.6 String Literals....................... A.3 Syntax Notation................................ A.4 Meaning of Identifiers......................... A.4.1 Storage Class......................... A.4.2 Basic Types........................... A.4.3 Derived types......................... A.4.4 Type Qualifiers....................... A.5 Objects and Lvalues............................ A.6 Conversions.................................... A.6.1 Integral Promotion.................... A.6.2 Integral Conversions.................. A.6.3 Integer and Floating.................. A.6.4 Floating Types........................ A.6.5 Arithmetic Conversions................ A.6.6 Pointers and Integers................. A.6.7 Void.................................. A.6.8 Pointers to Void...................... A.7 Expressions.................................... A.7.1 Pointer Conversion.................... A.7.2 Primary Expressions................... A.7.3 Postfix Expressions................... A.7.4 Unary Operators....................... A.7.5 Casts................................. A.7.6 Multiplicative Operators.............. A.7.7 Additive Operators.................... A.7.8 Shift Operators....................... A.7.9 Relational Operators.................. A.7.10 Equality Operators................... A.7.11 Bitwise AND Operator................. A.7.12 Bitwise Exclusive OR Operator........ A.7.13 Bitwise Inclusive OR Operator........ A.7.14 Logical AND Operator................. A.7.15 Logical OR Operator.................. A.7.16 Conditional Operator................. A.7.17 Assignment Expressions............... A.7.18 Comma Operator.......................... A.7.19 Constant Expressions.................... A.8 Declarations..................................... A.8.1 Storage Class Specifiers................. A.8.2 Type Specifiers.......................... A.8.3 Structure and Union Declarations......... A.8.4 Enumerations............................. A.8.5 Declarators.............................. A.8.6 Meaning of Declarators................... A.8.7 Initialization........................... A.8.8 Type names............................... A.8.9 Typedef.................................. A.8.10 Type Equivalence........................ A.9 Statements....................................... A.9.1 Labeled Statements....................... A.9.2 Expression Statement..................... A.9.3 Compound Statement....................... A.9.4 Selection Statements..................... A.9.5 Iteration Statements..................... A.9.6 Jump statements.......................... A.10 External Declarations........................... A.10.1 Function Definitions.................... A.10.2 External Declarations................... A.11 Scope and Linkage............................... A.11.1 Lexical Scope........................... A.11.2 Linkage................................. A.12 Preprocessing................................... A.12.1 Trigraph Sequences...................... A.12.2 Line Splicing........................... A.12.3 Macro Definition and Expansion.......... A.12.4 File Inclusion.......................... A.12.5 Conditional Compilation................. A.12.6 Line Control............................ A.12.7 Error Generation........................ A.12.8 Pragmas................................. A.12.9 Null directive.......................... A.12.10 Predefined names....................... A.13 Grammar......................................... Appendix B - Standard Library.................................... B.1.1 File Operations................................ B.1.2 Formatted Output......................... B.1.3 Formatted Input.......................... B.1.4 Character Input and Output Functions..... B.1.5 Direct Input and Output Functions........ B.1.6 File Positioning Functions............... B.1.7 Error Functions.......................... B.2 Character Class Tests: ................. B.3 String Functions: ..................... B.4 Mathematical Functions: ................. B.5 Utility Functions: ....................

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值