1. assume that HI and low are two integers, their values are between 0 and 15, and R is an eight-digit integer. If we want the lower four bits of R to be consistent with low, the higher four bits are consistent with HI, it is natural to write like this:
R = Hi 4 4 + low; unfortunately, this is wrong, because the addition operation level is higher than the shift operator, so it is actually equivalent to r = Hi "(4 + low );
So the correct method is: r = (Hi 4 4) + low or R = Hi "4 | low;
2. int calendar [12] [31] can be seen as an array with 12 elements. Each element is an array with 31 integer elements, each calendar [x] Of these 12 elements is the first address of the 31 element arrays. Therefore, it can be represented as * (calendar ar [4] + 7) on April 9, April 7) or * (calendar + 4) + 7) or calendar [4] [7];
3. The NULL pointer is not a null string. When the constant zero is converted to a pointer, the pointer cannot be removed from the reference ). in other words, when we assign 0 to a pointer variable, we absolutely cannot attempt to use the memory content that the Pointer Points.
The following statement is legal:
If (P = (char *) 0 )...... </P> <p> but the following statement is invalid: </P> <p> If (strcmp (p, (char *) 0) = 0 )...... </P> <p> the reason is that the implementation of the library function includes operations to view the content in the memory pointed to by its pointer parameter.
4. Boundary Calculation and asymmetric boundary.
The following method is recommended for loop writing:
Int A [10], I; </P> <p> for (I = 0; I <10; I ++) <br/> A [I] = 0;
Instead of writing it as follows:
Int A [10], I; </P> <p> for (I = 0; I <= 9; I ++) <br/> A [I] = 0; <br/>
For the pointer bufptr, we consider the preference of "asymmetric boundary" to point it to the first unoccupied character in the buffer, rather than to the last occupied character in the buffer, then we write the statement as follows:
* Bufptr ++ = C;
When the pointer bufptr is equal to & buffer [0], the content stored in the buffer zone is empty. Therefore, when the buffer is declared as null during initialization, you can write as follows:
Bufptr = & buffer; or write bufptr = buffer in a more concise manner;
Therefore, the number of characters in the buffer zone is bufptr-buffer, and the number of unused characters is N-(bufptr-buffer );
Now write the function bufwrite:
Void bufwrite (char * P, int N) </P> <p >{</P> <p> while (-- N >>= 0) </P> <p >{< br/> If (bufptr ==& buffer [N]) <br/> flushbuffer (); </P> <p> * bufptr ++ = * P ++; <br/>}</P> <p>}
The bufwrite function has two parameters. The first parameter is a pointer pointing to the first character to be written into the buffer zone. The second parameter is an integer representing the number of characters to be written into the buffer zone. Suppose we can call the flushbuffer function to write the content in the buffer, and the flushbuffer function will reset the pointer bufptr to point it to the starting position of the buffer. Note: In most C language implementations, -- You> = 0 is at least as fast as the equivalent n --> 0, and even faster in some C implementations.
The Code compares bufptr with & buffer [N], while the buffer [N] element does not exist! Array Buffer element subscript from 0 to N-1, so we use
If (bufptr ==& buffer [N])
Equivalent
If (bufptr> & buffer [N-1])
The reason is that we stick to the "asymmetric boundary principle": we need to compare the address of the first character after the pointer bufptr and the buffer. In fact, we do not need to reference this element, you only need to reference the address of this element.
5. There are only four operators in C Language (&, | ,? : And,) There is a specified order of value.
& | First, evaluate the left-side operands. Only when necessary can the right-side operands be evaluated. Operator? : There are three operands: In? In B: C, operand A is first evaluated, and then the value of operand B or C is obtained based on the value of. The comma operator first evaluates the left operand, then the value is discarded, and then evaluates the right operand.
The order in which all other operators in the C language evaluate their operands is undefined. In particular, the value assignment operator does not guarantee any order of value.
6. bitwise operators &, | and ~ The Processing Method of the operand is to treat it as a binary bit sequence and operate on each bit separately. Operator & operator | when the value of the left-side operand determines the final result, it does not evaluate the value of the right-side operand.
7. the extern keyword is a reference to an external variable rather than a definition. The static modifier can restrict the scope of the variable to a source file, which is invisible to other files. We can define function g with the same name in multiple source files, as long as all function g is defined as static, or only one function g is not static.
8. If a function is declared or defined before it is called for the first time in every file that calls it, there will be no trouble related to the return value type. If a function is called before it is defined or declared, its return value type is an integer by default.
9.
# Include <stdio. h> </P> <p> main () <br/>{</P> <p> int I; <br/> char C; </P> <p> for (I = 0; I <5; I ++) </P> <p >{</P> <p> scanf ("% d", & C); </P> <p> printf ("% d ", i); </P> <p >}</P> <p> printf ("/N"); </P> <p>}
On the surface, this program reads 5 numbers from the standard input device and writes 5 numbers to the standard output device: 0 1 2 3 4
In fact, this program does not necessarily get the above results. On a compiler, the output may be:
0 0 0 0 0 1 2 3 4
The key to the problem is that C is declared as char rather than Int. When the program requires scanf to read an integer, it should pass it to the integer pointer. In the program, the scanf function obtains a pointer to a character. scanf cannot distinguish this situation. Instead, it only receives the pointer to a character as a pointer to an integer, and store an integer at the position pointed to by the pointer. Because the storage space occupied by integers is larger than the storage space occupied by characters, the memory near the characters will be overwritten.
The content stored in memory near character C is determined by the compiler. In this example, it stores the low-end part of integer I. Therefore, each time a value is read to C, the low-end part of I will be overwritten with 0, and the high-end part of I is originally 0, which is equivalent to setting I to 0 every time, the loop continues. When the file ends, the scanf function no longer attempts to read new values to C. Then, I can increase normally and terminate the loop.
10. In a microprocessor, data is always stored in a high-priority manner, but in the memory, the vendor may vary.
Big endian: the lowest address stores high bytes, which is called high priority. The memory starts from the lowest address and is stored in order. High numbers are written first. All processors store data in this order.
Little endian: the lowest address stores low-level bytes, which can be called low-level priority. The memory starts from the lowest address and is stored sequentially.
11. It is recommended that the macro parameters do not have ++; otherwise, many side effects may occur.
12. It is best to use a type definition to define a new type, instead of a practical macro. For example:
# Define T1 struct Foo *;
Typedef struct Foo * t2;
T1 and t2 are identical in concept and are pointers to the structure Foo. If you declare multiple variables, there will be a problem:
T1 A, B;
T2 A, B;
The first one is expanded to struct Foo * a, B;
In this way, a is defined as a pointer to the struct, and B is defined as a struct.
13. define the basic data type as a new data type so that the migration can be modified more.
14. Because a String constant can be used to represent an array of characters, you can replace it with a String constant where the array name appears:
(Char) (N % 10) + '0') can be replaced by "0123456789" [n % 10]
15. Macro for case-insensitive letter swaps
# DEFINE _ toupper (C) (c) + 'a'-'A ')
# DEFINE _ tolower (C) (c) + 'a'-'A ')