Knowledge increment of reading "C traps and defects"

Source: Internet
Author: User
Tags integer division

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 figurex+++++yWill be parsedx++ ++ +yAnd 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.1int 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 = &aThe 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 );
  • 3.6 how to calculate the array boundary correctly? Principle 1: consider the special cases in the simplest case; principle 2: Calculate the boundary carefully.
  • 3.6 why does the following code cause an endless loop? This is because when the memory address is decreased, a [10] is I.
    int i,a[10];for(i = 1; i<=10; i++)a[i] = 0;
  • 3.6 border programming skills: Use the first entry point and the first exit point to represent the Numerical range, that is, [low, high ). The effect is:
    • The value range is the difference between the two.
    • If the value range is null, the upper bound is equal to the lower bound.
  • 3.6--nGeneral Ration--The execution speed is faster.
  • 3.7 operator & | ensure that the two operands are evaluated from left to right. The order of the operands of other operators is not defined. For exampley[i] = x[i++]The result is undefined.
  • 3.9 how can I check whether a + B overflows?
    • if(a+b < 0)It is incorrect because the overflow behavior is undefined. The correct method is to convert the two to unsigned and int_max for comparison.
    • More clever method:if(a > INT_MAX - b)
Chapter 4 connection
  • 4.2int aIf 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.3setbuf(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.4typedef struct foo FOOTYPEIs 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)cInstead(unsigned)cThe latter converts C to int and then to unsigned Int.
  • 7.5 division is much slower than shift.
  • 7.7 integer division operation, only requiredQuotient X divisor + remainder = DivisorIn 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.