"C expert programming" reading record

This book is suitable for flipping through when you have nothing to do, and you should be able to read any page you open. Overall, it's more enjoyable to write, not as serious as the chapter titles of "C and Pointers" and "C's Pitfalls and Defects", and sometimes I think I'm reading a tech magazine. However, to understand the content of the book, it is necessary to have a higher C language foundation, and the content structure is relatively loose. If you feel that there is no logic, it may be that the language foundation is not mastered enough.


Chapter 1C: Through the Fog of Time and Space
  • The prehistoric stage of the C language: BCPL->B->New B->Early C
  • Early experience of C language (Comment: Historical reasons have caused some language features of C language now)
  • Standard I/O library and C preprocessor
  • K&R C    ——Kernighan 和 Ritchie
  • ANSI C Today
  • It's great, but is it compliant: non-portable code, bad code, portable code
  • Compilation limit, the ANSI C standard places a limit on the minimum length of a program that can be successfully compiled
  • The structure of the ANSI C standard, compared with K&R C, the most important new feature is the prototype , this feature is taken from C++.
( Prototype: Make the types of the formal parameters as part of the function declaration. This way not only the function name and return type are known, but also all the parameter types.)
Non-portable code : It is up to the compiler designer to decide what to do (i.e. different compilers may behave differently, but all are correct) - undetermined ( P.S. a program may In one compiler the standard is followed, but in another compiler it is not. )
Bad code : Unspecified what to do in some abnormal situation (i.e. you can take any action: do nothing, issue a warning message, abort the program) - undefined  ( P.S. constraints: A restriction or requirement that must be followed. If not followed, the program becomes undefined. )               
 

Chapter 2 It's Not a Bug, It's a Language Feature
  • Details of programming languages, Fortran language classic errors, the difference between DO 10 I=1.10 and DO 10 I=1,10.
One way to analyze programming language defects is to classify all defects into 3 categories: what should not be done, what should be done not done, and what should be done but not appropriate.
  • overdo
    • fall through in switch statement  
    • Adjacent string constants are automatically merged  
    • Too many default visibility, i.e. default function scope is too wide

  • Mistakes , misleading or inappropriate character in language
    • Overloading, many symbols and even keywords are overloaded
    • Some operators have wrong precedence (the ANSI C standard does not take precedence for various historical reasons)

  • Don't do too much , features the language should provide but don't
    • white space problem
    • Annotation style
    • Compiler date is broken
    • Lint programs should never be separated. Lint programs look for bugs, use them early, and use them frequently.
 
 
Chapter 3 Analyzing C Declarations
  • A language only a compiler would like, declaring
  • How the declaration is formed, at least one type specifier + one and only one declarator + zero or more declarators + one semicolon
  • Priority rules, parentheses -> suffix operator -> prefix operator, the priority is from high to low in this order
  • Analyzing C language declarations through diagrams
  • typedef can be your friend, introducing a new name for a type instead of allocating memory for a variable
  • Difference between typedef int x[10] and #define x int[10]
    • Macro type names can be expanded with other type specifications
#define peach int
unsigned peach i; /* 没问题 */
typedef int banana;
unsigned banana i; /* 错误,非法 */
    • 连续几个变量声明中,typedef保证所有变量为同一种类,#define定义则无法保证
  • typedef struct foo {..foo;}的含义,不同的命名空间内使用同一个名字,bad habit
 
 
第4章 令人震惊的事实,数组和指针并不相同
  • 数组并非指针
  • 我的代码无法运行
  • /* file 1 */ int mango [ 100 ];
    /* file 2 */ extern int *mango;
  • 什么是声明,什么是定义
    • 声明,描述其他地方创建的对象
    • 定义,确定对象类型并为其分配内存,只能出现在一个地方
  • 使声明和定义匹配,指针的外部声明与数组不匹配,代码当然没办法运行
  • 指针和数组的区别
指针 数组
保存数据的地址 保存数据
间接访问数据,首先取指针内容,把它作为地址,然后从这个地址取地址。如果指针有一个下标[I],就把指针的内容加上I作为地址,从中提取数据
 
直接访问数据,a[I]只是简单的以a+I为地址取得数据
通常用于动态数据结构 通常用于存储固定数目且数据类型相同的元素
相关的函数为malloc().free() 隐式分配和删除
通常指向匿名数据 自身即为数据名


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686028&siteId=291194637