C has been used for a long time, but it is hard to say that c is very thorough at all points. Although c is not as complex as c ++, there are still a lot of difficulties: it is not how difficult they are, but that they do not use much at ordinary times, or they are contrary to people's first intuition, in addition, the limited understanding of the experience at the beginning of the course is hard to remember.
The following things may come from expert c programming or the network. Recently, it is found that basic classic books are frequently read for two reasons:
1. As your experience grows, your understanding may be different and your way of thinking may change. What you get is naturally new.
2. Early experiences were limited, and some points may not be fully understood at all. Now you can understand more deeply.
This book is another example of code Daquan. I flipped through it a few days ago and have different understandings.
Enter the subject:
1. Comparison of signed and unsigned data:
Printf ("% d \ n", sizeof ('A'): The printed value is 4 (or the length of the int) instead of 1. Because c has A type improvement, it will first promote 'A' to the int type, and then pass it to sizeof. The parameters in the expression are upgraded to int or double, and then the operation is performed before cropping to obtain the value of the specified type.
If (-1 <= sizeof (int): the return value of sizeof is unsigned int.-1 is converted to unsignedint by type and then compared ..
Here, we need to increase the type and implicit type conversion. It occurs in the expression and function input parameters.
2. Memory size of enumeration: 4 bytes.
3. Local variables are also byte aligned:
E_T g;
E_T f;
E_T e = false;
Char c1;
Char c2;
Int i1;
Char c3;
Int i2;
Printf ("% p, % p \ n", & g, & f, & e, & c1, & c2, & i1, & c3, & i2 );
-- Indicates the complement bit.
4. The functions of # And #: # in the macro definition are to stringize the macro parameters that follow them (Stringfication ), simply put, a double quotation mark is added to the left and right sides of the macro variable referenced by the macro variable.
# Is called a concatenator to connect two tokens into one.
5. The floating point cannot be equal to or compared.
6. void foobar2 () indicates that there are multiple input parameters in the function. If this parameter is not generated, it must be void foobar2 (void)
7. global variables are initialized to 0, but local variables in the stack are not initialized.
8. inline functions and macros: inline functions are real functions, but they are optimized during compilation.
9. int a [5]; printf ("% x \ n", a); printf ("% x \ n", a + 1 ); printf ("% x \ n", & a); printf ("% x \ n", & a + 1 );
Last, & a + 1, & a indicates the array. Therefore, the array size should be increased to 4*5 bytes.
10. 10U indicates an unsigned number 10.
11. The shift operation has a lower priority than the four operations.
12. Shift n places left, equivalent to the Npower of multiplication and 2. The right shift is equivalent to the N power of 2.
13. pointers and arrays:
1) void fun (char buf [100])
{
Printf ("% d, \ n", sizeof (buf ));
}
The printed value is 4 instead of 100.
2) in a file, char p [10] = "";
Declare in another file: extern char * p;
Then, in the declared file, sizeof (p), the answer is 4. That is, sizeof calculates the declared type.
3) for the compiler, an array is an address, and a pointer is an address.
4) the compiler converts all array names used as function parameters to pointers. In all other cases, the declaration of an array is an array, and the pointer is a pointer.
Rules for the same conditions of arrays and pointers:
1. the array name in the expression (different from the declaration) is treated as a pointer to the first element of the array by the compiler.
2. The subscript is always the same as the pointer offset.
3. In the function declaration, the array name is treated as a pointer to the first element of the array by the compiler. The compiler completes this operation. The reason is the efficiency. This is because the reference is passed instead of the value. The value must be copied. This also shows that sizeof is operated in the Assembly.
The behavior of arry [-1] is undefined.
Summary:
1) access to a in the form of a [I] is always rewritten by the compiler as a form like * (a + I.
2) the pointer is always a pointer. You cannot rewrite it into an array, but it can be accessed through an array.
3) arrays, as function parameters, will be rewritten as pointers by the compiler.
4) the pointer and the array must be paired.
14. Declaration and definition: there can be multiple declarations with only one definition. Definition is a special declaration, which allocates memory for the object. The Declaration is a common declaration that describes the objects created elsewhere.
Declared priority rules:
A: read from his name in order of priority:
B: Priority:
1. The section enclosed in parentheses in the statement.
2. suffix operators:
Brackets () indicate a function;
Square brackets [] indicate an array;
3. prefix OPERATOR: * indicates the pointer to something;
4. If the const parameter is followed by the variable, the variable cannot be modified, and the variable pointing to the type cannot be modified.
15. Multi-dimensional array:
A [2] [3]: a is an array with two elements. Each element is an array with three elements.
Memory layout: a [0] [0], a [0] [1], a [0] [2], a [1] [0]... the address keeps increasing.
Multi-dimensional array. The array is used as the function parameter and is converted to an array pointer. The array pointer is also a row pointer. Essentially a pointer.
16. The default byte alignment of the struct generally meets three criteria:
1) The first address of the struct variable can be divisible by the size of its widest basic type member;
2) The offset (offset) of each member of the struct to the first address of the struct is an integer multiple of the member's own size. If necessary, the compiler will add the internal adding between the members );
3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the trailing padding after the last member ).