C language deep anatomy Reading Notes (1. Secret of keywords)

Source: Internet
Author: User

Before starting this study note, let's talk a few additional questions. In fact, for the deep anatomy of C language, I have been reading this book for a while and I have never been able to write this blog. Coincidentally, I just watched the C-language video of Guo embedded Tang and thought the two were just the same, so I recorded them together. After updating these seven chapters of study notes, I plan to take a rough look at some of the remaining C language books.

Knowledge in this section: 1. there are 32 keywords in C language: auto, Int, double, long, Char, short, float, unsigned, signed, sizeof, extern, static, Goto, if, else, struct, typedef, union, Enum, switch, case, break, default, do, while, const, register, volatile, return, void, for, and continue. Note: define and include are not keywords and are pre-processing commands. 2. Definition and Declaration: It is defined to create an object and allocate memory until now. For example: int A; the Declaration tells the compiler that there is such an object in the program and no memory is allocated. For example, extern int A; 3. Register keywordThe Defined variables cannot be used for address fetch (&), because for X86 architecture, the addresses are in the memory, not in the registers, therefore, it is meaningless to retrieve the address of the register. It should be noted that the variable defined for register should be assigned a value smaller than the register size. Note: register only requests register variables, but it is not necessarily successful. 4. Keyword static: = There are two ways to use static:. modify variables: static global variables and static local variables both have a feature that they cannot be called outside the scope or external files (even if extern is used ). The reason is that it is stored in the static storage area. There is another problem with the static local variable in the function, that is, it has a static storage area. Even if the function ends the stack area, the value of this variable does not change. Static int I = 0; this is an initialization statement instead of an assignment statement, so it is different from I = 0. B. modifier: it is defined as a static function, so that the function can only be used within the file, so that the function names in different files are not afraid of Repeated names. The reason is also the same, that is, all static modification is in the static storage area. -Static code:
#include <stdio.h>#include <stdlib.h>int main(void) {static int j=0;int k;void fun1(){j=0;j++;printf("fun1 %d\n",j);}void fun2(){static int i=0;//i=0;printf("fun2 %d\n",i);i++;}for(k=0;k<10;k++){fun1();fun2();} return 1;  }
5. Keyword sizeof:The sizeof keyword is not a function. Here are two examples:. int I; printf ("% d \ n", sizeof I); it can be seen that sizeof is the keyword B. sizeof (fun (); do not call the fun function because sizeof is a description completed during pre-compilation and is the code with the keyword sizeof:
#include <stdio.h>#include <stdlib.h>void fun(int b[100]){printf("sizeof(b) is %d\n",sizeof(b));}int main(void) {int *p=NULL;int a[100];int b[100];printf("sizeof(p) is %d\n",sizeof(p));printf("sizeof(*p) is %d\n",sizeof(*p));printf("sizeof(a[100]) is %d\n",sizeof(a[100]));printf("sizeof(a) is %d\n",sizeof(a));printf("sizeof(&a) is %d\n",sizeof(&a));printf("sizeof(&a[0] is %d\n",sizeof(&a[0]));fun(b);return 1;}

6.Keyword: if:
A. Comparison of the bool type: flase is 0. True is not necessarily 1. Therefore, if (bool_num); If (! Bool_num); do you need to pay attention to the comparison between float type and 0: you cannot directly compare the float type and define the precision. In fact, you must pay attention to this problem when comparing float type and float type, that is, you cannot directly compare the precision, the reason is related to the floating point storage format. Because the float effective bit is 6 bits, it is unknown if it exceeds 6 bits, so it cannot be compared directly. For the same reason, a large floating point number cannot be used to add a small floating point number. This addition may not be reflected. B. note that there is an empty statement after the if statement, so it is best to use null when using the null statement; C. when using the if else, you should first handle the normal situation (with a high probability) in the IF, and handle exceptions in the else. This is a good habit of looking at the code comfortably. 7.Keyword switch, case:Note that the case should be followed by constants and constant expressions of the integer or numeric type. It is best to install letters or numbers in sequence after the case. Handle normal conditions first, and then handle exceptions later. 8.Keyword void:The general purpose of void * is to receive pointers of any type. If the pointer type of the input function is uncertain, void * is generally used to receive pointers of any type. When the void * pointer is assigned to another pointer as the right value, it must be forcibly converted because the void * pointer type is not fixed. In GNU, void * p ++ is the same as char * p ++.Note: The difference between strcpy and memcpy is that strcpy is char * memcpy is void *.Therefore, strcpy assigns values to strings and memset assigns values to the entire memory.9.Keyword extern:Extern has two usage methods: one is to declare a variable or function of an external definition, and the other is that extern C tells the compiler to compile in a standard C language 10.Keyword return:When using return, note that the pointer in the stack cannot be returned, because the stack is reclaimed after the function body ends. In fact, a pointer cannot be returned to return a piece of memory. It is okay to return a pointer or address because return is copy and then returns, but if the pointer points to the memory in the function stack, it is very likely that the function will be reclaimed after it is completed !!! Return; generally, the returned value is 1, depending on the compiler. 11.Keyword const:A. const is used to define Read-Only variables. do not define variables rather than constants. True constants are # define and enum. B. On the 35th page of Mr Chen zhengchong's book,It is said that the compiler does not allocate memory space for normal const Read-Only variables, but stores them in the symbol table, which makes it a value during compilation without the operation of storage and read memory, this makes it highly efficient and saves space.I don't know much about it. I don't want to understand it this time (because it means that the global read-only variable modified by const is in the static zone, and I agree with it too much )~~~ Hey, C. Actually, const is modifying the variable. Then this variable cannot be treated as the left value. If it is treated as the left value, the compiler reports an error !!! D. in fact, the most difficult distinction between const and const is that the memory for these four cases is very simple, that is, to see who const is close to, is const * P, or * const P, or const * const P, so it is easy to see who const modifies. E. But the variable modified by const can be changed through the pointer. F. the const modifier function parameter indicates that you do not want to change the parameter value in the function body. For example, in strcmp and other functions, the const char * g is used. the Return Value of the const modifier function indicates that the return value cannot be changed and is mostly used to return pointers:

cosnt int* func(){      static int  count  =  0;      count++;      return &count;}

H. To check who const modifies and who remains unchanged, you can remove the type and view the Code as follows:

struct student{}*str;const str stu3;str const stu4;

STR is a type, so when removing the type, it should be changed to const stu3 and const stu4, so the pointer stu4 and stu3 should not be assigned a value.
12.Keyword volatile:
The keyword volatile is embedded. It must be special. Remember that when we use this keyword, we use it for the first time, when we use the video of Jack Wei Dongshan and arm bare metal. Volatile tells the compilation not to optimize the variable, but to take the value directly in the memory. It is generally used to assign values to registers or modify variables that may be accessed by multiple threads.

Note: const volatile int I; a read-only register should be defined. 13. Keyword struct:A. for the size of the empty struct, the output of VC and GCC is different, VC is 1, GCC is 0, and VC's definition of the struct is also different from that of GCC, the C ++ standard in VC extends the role of struct, while the GCC standard is pure C, which is based on the standard C language. B. struct another useful thing here is flexible arrays, which is very interesting. I have already elaborated on the static linked list of the data structure. Here I just record it, not detailed. 14. Keyword union:One function of union is to determine whether a PC is stored on a large or small end, and x86 is stored on a small end. This is determined by the CPU. Arm (determined by the memory controller) is as small as x86. The following is an example of a Large-end and small-end. The Code is as follows:
# Include <stdio. h> # include <stdlib. h> int main (void) {int A [5] = {1, 2, 3, 4, 5}; int * P = (int *) (& A + 1 ); // Add an array pointer to perform normal pointer operations until the end of the array int * D = (int *) (INT) A + 1 ); // address plus one is not a pointer operation // printf ("% x \ n", * (char *) (INT) A + 1)-1 )); /* because it is a small-end storage, the high address 0x00 0x00 0x00 0x02 0x00 0x00 0x01 low address * // * becomes 0x02 0x00 0x00 0x00 */printf ("% x, % x ", P [-1], * D ); /* The second value is the stored 0x02 0x00 0x00 0x00 low address, so it is 2000000 */int A = 0x11223344; char * P = (char *) (INT) & A); printf ("% x \ n", * (p + 0 ), P + 0); printf ("% x \ n", * (p + 1), P + 1); Return 0 ;}

The following is an example of using Union to determine that a PC is a large-end and small-end. The Code is as follows:

#include <stdio.h>#include <stdlib.h>union{int i;char a[2];}*p,u;int main(void) {p=&u;p->i=0x3839;printf("%x\n",p->i);printf("a0p=%x,a1p=%x\n",&(p->a[0]),&(p->a[1]));printf("a0=%x,a1=%x\n",p->a[0],p->a[1]);return 0;}

15.Enum Keyword:
Enum is actually an int type, which is used to save enumerated constants. Enum Enumeration type. This is a real constant. Enum is generally used to define constants. # Define is a macro definition that is simply replaced during pre-compilation. # The define macro definition cannot be debugged, And the enumerated constants can be debugged. # The define macro definition has no type information. The Enumeration type is a constant with type information and is int type. 16.Typedef Keyword:
A. typedef is used to rename an existing data type. B. typedef does not generate a new data type C. the type that typedef redefines cannot be extended by unsigned or signed because it should be fully defined when typedef defines the new type. Unsigned int cannot be split.

typedef  unsigned  int   int32;
D. Differences between typedef and # define: typedef uses aliases for existing types, while # define only replaces simple characters. Differences: # define pchar * pchar P3, P4; // P3 is char * type p4 is char type
Typedef char * pchar; pchar P1, P2; // both P1 and P2 are char *
E. I forgot a knowledge point. Hey, the program is as follows:
typedef struct student{}str,*str1;
Str1 ABC; is to define a struct student * type
Str abc; defines a struct student type F. const and typedef still have two questions missing. In the <C ++ Study Notes (1. c To C ++ upgrade)> The last (8.. 17. Keyword: A. Long Loop should be in the innermost layer, which can reduce the direct switching between each layer B. See the differences between the following two sections of code:
Procedure 1: for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) {for (k = 0; k <p; k ++) {C [I] [J] = A [I] [k] * B [k] [J] ;}} Program 2: for (I = 0; I <m; I ++) {for (k = 0; k <p; k ++) {for (j = 0; j <N; j ++) {C [I] [J] = A [I] [k] * B [k] [J] ;}}
From the perspective of the program, the two implement the same function, but the difference is that the second and third layers exchange the position cyclically. However, the gap between them is huge. This requires the CPU cache. Each time the CPU accesses the memory, it first reads data from the memory into the cache, and then retrieves data from the cache. However, the cache size is limited, so only a portion of the cache is entered. Let's look at this program c [I] [J] = A [I] [k] * B [k] [J]; we all know that the two-dimensional arrays in C are arranged in one dimension in the memory. If we put the K loop on the third layer, the cache is basically useless and data needs to be retrieved from the memory every time, after the switch, the data obtained from the cache can be reused multiple times.
. Therefore, the second method is highly efficient. 18. Keyword CHAR ):There are two types of char: unsigned char (range: 0 ~ 255) and signed Char (range:-128 ~ 127) One is signed, and the other is unsigned. In computers, data is stored in the form of data completion codes. Therefore, for unsigned INT: the highest bit is not considered, and the original code is equal to the complement code. Then let's talk about the problem of crossing the border. For an unsigned char I; we give I = 256; this is obviously out of the border, and I is from 0 to 255, then what is the complement of 256 and then the lower eight bits in the complement is the value of I. The 256 complement is 1 0000 0000, so the value of printf ("% d \ n", I); will be 0. If I =-1;-1 is 1111 1111, 255 is printed. What is the cross-border of a char type? Char I; we give I = 129; 129 is a positive number, and its complement is the original code: 1000 0001, but it is char type. What is 1000 0001 in char type, yes-127. Therefore, printf ("% d \ n", I); returns-127. If I =-129, its complement code is 0111 1111, so it prints 127. If it is I = 259, we will take its complement code as low as eight bits. 259 of the complement code is 1 0000 0011 so the printed code is 3. In the last example, if I = 385, its complement is 1.
1000 0001, take the lower eight bits is 1000 0001, so the print should be-127. In fact, whether it is signed or unsigned, the principle is one. Convert the data into a complement code, take the lower eight bits, and then compare them in the above figure. For an exercise, the Code is as follows:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char a[1000];int i;for(i=0; i<1000; i++){a[i] = (-1-i);}         while(a[i]){ printf("%d\n",a[i]);i++;}printf("%d\n",strlen(a));return 0;}

What is the print structure? The answer is that the analysis steps are the same as above. Let's calculate it by yourself !!! In fact, the principle of int out-of-bounds is the same as that of char. 19. one question about the tab key: the number of tab keys in different Editors is different, generally four characters and two bytes. Note that, to ensure the code format is neat, we recommend that you set tabs or spaces. Problems left over in this section: 1. the implementation of printf is actually a problem of variable parameters. Looking at the Linux source code, another problem is the transfer of characters, such as char P =. 2. Why is the floating point storage format 6 bits valid and how are decimals saved.

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.