After reading "C traps and defects", I can't help but flip it over and record what is not in line with my inertial thinking. It records the increment of knowledge, which is the traffic of the past few days, rather than the inventory.
This book was written before asci c/c89 was made.
Chapter 1 lexical traps
- 1.3 Use greedy policies when parsing symbols in C language, as shown in figure
x+++++y
Will be parsedx++ ++ +y
And compilation error.
- A character enclosed by a single quotation mark (1.5) represents a corresponding integer. for compilers using the ASCII character set, 'A' and 0141 and 97 have the same meaning.
- Exercise 1.1 nested comments (such
/*/**/*/
Is only allowed in some C compilers, such as gcc4.8.2 is not supported during compilation.
Chapter 2 syntax traps
- 2.6 else always combines with the nearest unmatched if in the same bracket
Chapter 3 semantic traps
- 3.1
int a[12][31]
Represents an array with a length of 12. Each element is an array with a length of 31.
- 3.1 If the array name is replaced by the array name where the pointer is needed, the array name is regarded as the pointer to the element whose subscript is 0,
p = &a
The format is invalid (gcc4.8.2 is only a warning ).
- 3.2 how to connect two strings S and T? The details are very important. The answer in the book is as follows:
Char * r, * malloc () // The original name cannot directly declare an array with the sum of S and T Length, but c99 can declare a variable-length array, yes. // remember to add the length to 1R = malloc (strlen (s) + strlen (t) + 1); // you must determine whether the memory is allocated successfully if (! R) {complain (); exit (1);} strcpy (R, S); strcat (R, T );...... // release rfree (r) after completion );
Chapter 4 connection
- 4.2
int a
If it appears outside of all function bodies, the declaration and definition (bucket allocation) are completed ). Whileextern int a;
It only indicates that the bucket A is allocated elsewhere, not the definition. Therefore, it must be defined somewhere else, either in the same or different source files.
- 4.3 The static modifier can restrict the scope of a function or variable to a source file without conflict with the number of identical names in other files.
- 4.5 declarations and definitions must be strictly the same, while arrays and pointers are different.
- 4.6 how to avoid inconsistency between declarations and definitions? Follow the rule that "each external object is declared in only one place. It is usually placed in the header file. All source files that use the object in addition must include this header file. files that define this object should also include this header file.
Chapter 5 database functions
- 5.1 getchar () returns an integer. the return value cannot be assigned to the char type variable and then compared with EOF. Because EOF is defined as-1, it should be assigned to the int type variable.
- 5.2 if you want to perform continuous read and write operations on the file, the fseek function must be inserted in the middle.
- 5.3
setbuf(stdout, buf);
You can set the char array pointed to by BUF as a buffer to change the output cache size.
- 5.3 It is wrong to copy the stdin content to the stdout program using the buffer zone, because the buffer content is written until the buffer zone is full or fflush is called. You can declare the Buf as static or malloc In the heap to prevent Buf from being cleared after the main function is completed.
- 5 .. 1 when a program ends abnormally, the last part of the program output is often lost. You can use setbuf to point to a null pointer as a buffer zone.
- 5. 2 putchar/getchar use macro implementation in stdio. H. If stdio. H is not included, it is very likely that it can still be run, but the corresponding function is used instead, reducing the speed.
Chapter 6 pre-processor
- 6. A macro is only used for text processing. It is an expression, not a function or statement.
- 6.1 macro definition it is best to enclose each parameter and the entire expression in parentheses to prevent errors.
- 6.2 If an operand is used in two places, it will be evaluated twice. Solution: The operands should have no side effects. Convert the macro into a function.
- 6.2 macros may generate very large expressions.
- 6.3 It is very troublesome to use the semicolon of a macro. A correct implementation of assert is as follows:
#define assert(e) ((void)((e)||_assert_error(__FILE__,__LINE__)))
- 6.4
typedef struct foo FOOTYPE
Is a Type Definition Statement that defines a new type.
Chapter 7 portability Defects
- 7.4 compiler implementations may treat characters as signed or unsigned. When char is converted to int, the result is undefined. You can use unsigned char to avoid this.
- 7.4 use
(unsigned char)c
Instead(unsigned)c
The latter converts C to int and then to unsigned Int.
- 7.5 division is much slower than shift.
- 7.7 integer division operation, only required
Quotient X divisor + remainder = Divisor
In most cases, when negative division is implemented, only the remainder is the same as the positive or negative number of the divisor, and the operator is irrelevant to the symbol of the divisor. N should be considered as the unsigned number.
- 7.9 toupper/tolower functions all use int-type parameters. when implementing this function, you must check whether the input meets the requirements. It is very fast to use the set bit.
- 7.11 requires a long number to be output by bit. Consider: you cannot evaluate-N and may overflow (Boundary Condition). You should convert n to negative and then process it. If the symbol of the remainder is unknown, normalization should be performed.
- 7 .. 2 The atoi function converts a string to a long integer and should be processed as a negative number to avoid overflow.
For more information, see focustc. The blog address is http://blog.csdn.net/caozhk.