I wrote a few notes about the blind spots in my C language. I only thought that I had experienced and had errors.
1. implict declartion of function indicates that there is no function declaration during preprocessing, but this function is compiled and called in other functions. The compilation can complete the link, but it will provide the warning, which must be affirmed in advance.
2. sizeof is an operand operator. It cannot be directly operated on and obtained from the address. Is a function automatically calculated by the compiler during preprocessing. It is similar to macro definition. Sizeof is not a function. It is processed during compilation.
During compilation, the compiler deduce the sizeof parameter type. sizeof determines an integer based on this type, so it can be used as a constant.
A function cannot be used as a constant. A function can only be evaluated at runtime.
In 3.1.C, when a struct pointer is assigned NULL, the struct value is not allowed until it is modified again. Because NULL = 0, the content at 0 is obtained, when the program is running, the segment error Segmentation fault will appear. Do not set the NULL address value. This error makes it best not to pass in Pointer variables when calling a function that requires pointers, unless it comes with an address such as malloc and calloc, otherwise this address is 0 by default, once a value is assigned within the function, a segment error occurs. Segment errors are often caused by improper operations on the memory, so that the program occupies the memory during operation. After detecting the system, the program is forcibly terminated to prevent the system from crashing.
2. when using malloc, You need to determine whether to allocate the required memory size. Otherwise, the memory that exceeds the program access is often not the value you imagined, the system has self-reclaimed these memories, leaving only the part you applied. Maybe you can access this memory again, but the final result must be that the memory has been recycled and cleared. The benefit of the system is that it is not the area in the heap memory linked list, but it is recycled by itself, make the system more efficient.
4. Excellent programming habits in C # ifndef _ H_FINGER_API_H _
# Define _ H_FINGER_API_H _
# Ifdef _ cplusplus
Extern "C "{
# Endif
# Ifdef _ cplusplus
Extern "C "}
# Endif
This advantage is that when the defined header file appears multiple times in a c source file, there will be no repeated function declarations, at the same time, c can be easily transplanted to c ++.
5 in C language, when the struct pointer is assigned NULL, the value of this struct is not allowed until it is modified again, because NULL = 0, take the content at 0, when the program runs, a segment error occurs. Segmentation fault,
Do not set a NULL address value.
6, for (n = 0; n <nLen; n ++) and for (int n = 0; n <nLen; n ++) the difference between the two is that the n of the latter can only take effect in the for region, and the former is valid in the entire function region. N of the latter cannot be used outside. In c, the former is ideal. C99 standards
7. You can also use memcpy to copy temporary cache data during inter-function data transfer. Remember that you cannot overwrite the address of a temporary cache area to an input pointer variable in the function. It is meaningless because the content is cleared after the end (unless the content requested by malloc is used)
8. Can global variables be defined in the. and h header files that can be contained by multiple. C files? Why? Well, you cannot use a. h file containing a global variable (defined as initialization) in multiple. c files, because every file domain has this global variable. During the compilation of the connection, the compiler recognizes two global variables and assigns addresses. In this way, a connection error occurs before being added to the global variable area. Solution: 1. Declare extern int a in. h or in other. File c declares extern int. In this way, no error will be reported when the global variable is compiled. in file c, when define or Initialization is defined. if c is just a declaration, no error will be reported for this global variable. Generally, the memory will be allocated if the variable is defined. If multiple declarations appear, the defined one is used as the global value by default. 2. Add static to both the same variables. Because it is only valid in your own file domain, the usage of the function is similar.
The key is that the two initial values (that is, definitions) of global variables defined in. h are not allowed )!, Because after the value is assigned in. h, when multiple files are used, the variable initialization value is equivalent to twice. This is not allowed.
9. encoding. In the. h header file, create a declaration that only functions and variables are used. In the. c file, the specific implementation, and the definition of global variables.
10 all global variables and functions without the static prefix have global visibility, and other source files can also access
In 11Linux, the file line break is \ n (0x0a), which is different from \ n \ r (two bytes) in the window)
In Linux, The EOF is-1 and the binary file 0xff. (-1) EOF is the end mark of the text file. In a text file in window, data is stored in the form of the ASC Ⅱ code value of the character. The ASC Ⅱ code of a common character ranges from 32 to 127 (decimal ), the hexadecimal code of EOF is 0x1A (26 in decimal format). Therefore, you can use EOF as the end mark of a file.
1. Only files excluding control characters (excluding line breaks) can be read in text mode (<32)
2. text will be translated for line breaks. For example, in windows, translate/r/n into/n, and translate/n into/r/n. In unix, translate/n from the file into/n, and translate/n from the written file into/n.
12. If the pointer to the input function needs to be used, it is necessary to perform NULL detection. 13. The statement in C language is not necessarily a statement, int a; the system will define the default value. But the definition of int a = 1 must be defined. There is also extern int a; it must be a declaration, which is defined elsewhere. Here it is just a reference. The declaration with the extern keyword is not defined and memory is not allocated (in fact, memory is not allocated ). In fact, it only claims the names and types of variables that have been defined in other files. A variable can be declared multiple times, but can only be defined once. The declaration can be initialized only when it is also defined, because only the definition will allocate memory.
14. Before using an array or some struct, it is best to initialize it {0 }. memset or bzero, because the initial value of the stack space allocated by the compiler is not 0 within the local function. you need to initialize it as the required default value first, otherwise it may affect your subsequent programs, for example, Segmentation fault (usually the content block of the comparison operation for memory operations is smaller than the allocated size)
15 missing braces around initialize this problem is that multiple arrays in the structure are not initialized during initialization, so this error occurs, such as typedef struct Usr_Info {
Unsigned char ID [ID_LEN];
Unsigned char Finger [FMD_LEN];
Unsigned int finsih_flag; // 1: one usr info get finsihed
} USR_INFO; When initializing USR_INFO one = {0}, a warning is displayed. You need to initialize each part of the array, so {0}. {0}. 0 }. Features of C99
16 ansi c illustrates three functions for dynamic bucket allocation
(1) malloc allocates a storage area with a specified number of bytes. The initial values in this bucket are uncertain.
(2) calloc is an object of the specified length and is allocated with a storage space that can accommodate the specified number of objects. Each bit in the space is initialized to 0.
(3) realloc: change the length (increase or decrease) of the previously allocated area ). When the length is increased, you may need to move the content in the previous distribution area to another area that is large enough, and the initial value in the new area is not sure.
17 when a [] is used as a function parameter, it degrades to a pointer, that is, like * a, so that the write seems clearer.
18. After the static declared function is called by the function in the same file, the non-static function can still be called by the function in other files. Thus, it can be said that the static function is called indirectly.
19. How to print the long data type: printf ("% I64u \ n", a); or % I64x. lld to print the 64-bit data type. Unsigned long: lld, 64-bit long unsigned int: ld = unsigned long int.
20. Fread (opened in r read mode), fwrite (opened in a append mode), if the operating binary file is added as rb, AB
21. When the parameter is added to the stack, if the parameter is not int enough, it is extended to int and then added to the stack.
For example, char (-127) is extended to int (-127)
That is, scale from 10000001 to 11111111, 11111111, 11111111, and 10000001.
Then you use % u, that is, you can use 11111111 11111111 11111111 10000001 as an unsigned int. Then, of course, it is 4294967169.
22. Regarding the type upgrade and signed extension, I have read the last section of section 3.5 "How compiler handles type conversion" in Chapter 15 of "Linux C one-stop programming, the last section in the section says:
"In the last example, convert the short type to the int type, corresponding to the" signed integer to signed integer "in the table. The value should remain unchanged after the conversion. How can we keep the value unchanged? Is it enough to add 16 zeros at a high position? If the original value is-1, The hexadecimal format is ffff. To convert-1 to the int type, the hexadecimal format must be ffffff. Therefore, you must add 16 1 s instead of 16 0 s at a high position. In other words, to maintain the value unchanged, it is determined by the original symbol bit to add 1 or 0 in the high position. This is called
Symbol Extension(Sign Extension )."
This is signed integer to signed integer. Why does signed char use symbol extension when it is converted to an unsigned int ??
23. Use \ in macro definition to block line breaks, so that the subsequent content can be the content of the macro definition block until the macro content ends when a line break occurs.
24. sizeof operations for pointers and static Arrays
Pointers can be viewed as one type of variables. The sizeof operation result for all pointer variables is 4.
Note:: Int * p; sizeof (p) = 4;
However, sizeof (* p) is equivalent to sizeof (int );
For static arrays, sizeof can directly calculate the array size;
For example, int a [10]; char B [] = "hello ";
Sizeof (a) equals 4*10 = 40;
Sizeof (B) is 6;
Note:: When an array is used as a type parameter, the array name is used as a pointer !!
Void fun (char p [])
{Sizeof (p) equals 4}
25. C language compound operation: the principle is to assign values from right to left, which can save memory overhead a * = a; a =-a; a + = a; a/=, a | = a; a & = a, a ^ = a; a = ~ A (inverse); the reverse value of a negative number is different from that of a variable number. Obtain the opposite each bit, including the sign bit, and perform the inverse operation. The negative value is the complement code after the original value is converted. If the original number is positive, the positive value is a negative value and the original number is negative, the original code of positive number exists (the original code of positive number is the same as the complement code)
26. volatile in C Language informs the compiler that this variable should not be optimized by the compiler to indirectly access variables in registers. In any situation, direct access is required (to prevent special changes, which often refer to hardware-caused changes). volatile is easy to lose and unstable. the volatile variable allows modifications to content other than programs, such as hardware. 2. when you access the data, you can directly access the content at the address, that is, the optimization of the access speed through cache is canceled.
27 int bit0: 1
Bit Field means that when information is stored, it does not need to occupy a complete byte, but only needs to occupy a few or one binary bit. To save storage space and simplify processing, the C language provides a data structure called "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, we can use a single byte binary field to represent several different objects.
typedef union{
Unsigned char value;
Struct
{
Unsigned int bit0: 1;
Unsigned int bit1: 1;
Unsigned int bit2: 1;
Unsigned int bit3: 1;
Unsigned int bit4: 1;
Unsigned int bit5: 1;
Unsigned int bit6: 1;
Unsigned int bit7: 1;
} Bit_field;
} PORT;
28. fstat obtains basic information such as the file size, and ftruncate changes the file size.
Change ftruncate (fd, length) to the length of length.