回顾一下C语言的经典教材K&R,顺便把读书的笔记记在这里,以便以后需要时,不用再从头翻起。
这本书是2007.3.30网上买的,看了两遍,这次再回顾一遍
Introduction:
C is a general-purpose programming language.It has been closely associated with the Unix system where it was developed, since both the system and most of the programs that run on it are writen in C.
Many of the important ideas of C stem from the language BCPL and B.
Similarly, C offers only straightward, single-thread control flow: tests, loops, grouping, and subprograms, but not multiprogramming, parallel operations, synchronization, or coroutines.(C语句仅提供单线程控制流:检查、循环、分组、子程序,不支持多线程、并行操作、同步与协同控制)
C is independent of any parcular machine architecture.
C is not a strong-type language, but as it has evolved, its type-checking has been strengthened.
C, like any other language,has its blemishes.Some of the operators have the wrong precedence; some parts of the syntax could be better. Nonetheless, C is a expressive programming language.
Chapter 1: A Tutorial Intruduction
the C compiler will produce an error message because printf never supplies a newline automatically, so several calls may be used to build up an output line in stages.
In C, all vars must be defined before they are used, usually at the beginning of the function before any executeable statements.
Array subscripts always start at zero in C.
Function definitions can appear in any order, and in one or several source files, although no function can be split between files.
In C,all function arguments are passed "by value". this mean that the called function is given the values of its different properties rather than the originals. (C语言,传参,值传递)
Chapter 2: Types, Operators, and Expressions
Variables and constants are the basic data manipulated in a program. Declarations list the variables to be used. Operators specify what is to be done to them. Expressions combine variables and constants to produce new values.
Variable : Don't begin variable names with underscore, however, since library routines often use such names. Upper case and lower case letters are distinct.
Traditional C practice is to use lower case for variables names, and all upper case for symbolic constants.
At least the first 31 characters of an internal name are significant. For extenal names, the standard guaratnees uniqueness only for 6 characters and a single case.
short, int, long, signed, unsigned, float, double... Each compiler is free to choose appropriate sizes for its own hardware.
Constant : As in 12345678L is a long constant writen with a terminal l (or L). Unsigned constants are writen with a terminal U or u.
Declarations : All variables must be declared before use.
External and static variables are initialized to zero by default. Automatic varialbes for which there is no explicit initializer have undefined values.
Arithmetic Operators :
The % operator cannot be applied to float or double. The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands, as is the action taken on overflow or underflow.
Type Conversions : When an operator has operands of different types, they are converted to a common type according to a small number of rules, without losing information. In Section 6 of Appendix A states the conversion rules precisely.
notice: sometimes, -1L > 1U,because -1L is promoted to unsigned long.
Bitwise Operators : The shift << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be nonnegative.
Thus x << 2 shifts the value of x left by two positions, filling vacated bits with zero; this is equivalent to multiplication by 4. Right shifting an unsigned quantity always fills vacated bits with zero. Right shifting a signed quantity will fill with sign bits ("arithmetic shift") on some machines and with 0-bits ("logical shift") on others.
Precedence and Order of Evaluation :
C, like most languages, does not specify the order in which the operands of an operator are evaluated. For example , some statements like :
x = f() + g();
printf("%d %d/n", ++n, power(2,n));
a[i] = i++;
the order in which expression are evaluated is not specified.
Chapter 3: Conrol Flow
The control-flow statements of a language specify the order in which computations are performed.
There is no semicolon after the right brace that ends a block.
Switch : As a matter of good form, put a break after the last case (the default here) even thought it's logically unnecessary. Some day when another case gets added at the end, this bit of defensive programming will save you.
Chapter 4: Functions and Program structure
C has designed to make functions efficient and easy to use; C programs generally consist of many small functions rather than a few big ones.
External variables: IF a large number of variables must be shared among functions, external variables are more convenient and efficient that long argument lists. However, this reasioning should be applied with some caution, for it can have a bad effect on program structure and lead to program with too many data connection bewteen functions.
Scope rules: can u answer the questions ?
1. How are declarations written so that the variables are proprely declared during complation?
2. How are declarations arranged so that all the pieces will be properly connected when the prgram is loaded?
3. How are declarations organized so there is only one copy?
4. How are external variables initialized?
Initialization of an external variable goes only wih the definition.
Static variables: If a function is declarated static, however, its name is invisible outside of the file in which it is declared.
Register variables: The register declaration can only be applied to automatic variables and to the formal parameters of a function.
Initialization :
In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; automatic and register variables have undefined initial values.
For external and static variables, the initializer must be a constant expression; the initialization is done once, conceptually before the program begins execution. For automatic and register variables, it is done each time the function or block is entered. For automatic and register variable, the initialization is not restricted to being a constant expression: it may be any expression involving previously defined values, even function calls.
If there are fewer initializers for an array than the number specified, the missing elements will be zero for external, static, and automatic variables.
Recursion : C functions may be used recursively.
Macro Substitution:
If a # character in the replacement text, the combination will be expanded into a quoted string with the parameter replaed by the actual argument. Such as:
When this is invoked, as in, dprint(x/y); the macro is expanded into statement
The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion.