I finally read this book called The C Language Bible. The cover of the book is as follows:
The excerpt notes are as follows:
Page 16
The EOF value is-1, so the char type cannot store this value. Int type is required. Think of what we often write
While (scanf ("% d", & )! = EOF) In fact, scanf is an int type function. When the type matches, the return value is 1, and when the type does not match, the return value is 0, when no data is read, the returned value is-1, which is consistent with EOF.
Page53
X = F () + g (). This statement is not necessarily executed from left to right during compilation. The specific situation depends on the environment.
Printf ("% d", ++ N, power (n, 2) is not very correct.
Page84
Local variables, even if it is in IF, are correct.
int x;int y;f(double x){ double y;}
Page90
Macro replacement. If the parameter names in the replacement text are prefixed with #, they will be extended by the parameters replaced by the actual variable to a string with quotation marks.
# Define dprint (expr) printf (# expr "= % G \ n", expr)
Dprint (x/y)
Macro is replaced with printf ("x/y" "= % G \ n", x/y), that is, printf ("x/y = % G \ n ", x/y)
# It is a method to connect to the actual variable. Use this method to replace the text parameter. # It is deleted from the blank spaces before and after
# Define paste (Front, back) front # Back
Paste (name, 1) Create name1
Page115
Main (INT argc, char * agrv [])
The two parameters in main are used when the command line is used. The value of the first parameter is the number of variable elements in the command line when the program is executed, the second variable is a pointer to the string array, where each string corresponds to a variable.
For example, Echo Hello, world will print Hello, world
#include<stdio.h>main(int argc,char *argv[]){while(--agrc>0)printf("%s%s",argv[i],(i<argc-1)?" ":"");printf("\n"); return 0;}
Page138
Do not think that the size of a structure is equal to the sum of the size of each member. Because different objects have different alignment requirements, holes will appear in the structure. For example
struct {char c;int i;}
Eight bytes instead of five, sizeof [Note that sizeof is an operator rather than a function.] OPERATOR returns the correct value of structure size
Page147
All elements in union are located in the same region to ensure that the region size can accommodate the elements with the largest byte type. In a sense, union is also a structure, the offset of all its members to the base address is 0.
Supplement: Role of Association
Why is Union invented in C different from struct?
The difference between Union and struct is:
1. For a union variable, all member variables share a piece of memory. The size of the memory is determined by the maximum length of these member variables.
2. The memory allocated by union is continuous, which is very important.
For more details, here is the test code (Appendix 1 ):
Why do we need to invent union? This is determined by the Union feature, especially the second point above, because although the memory of the member variables in struct is independent, struct cannot guarantee that the allocated memory is continuous.
For example, the ax register of the CPU is divided into AH and Al. To assign a value to it, we can use Union:
Union _ ax_register {int I; // 4 bytes, union will allocate a 4-byte continuous memory unsigned short Ah; // 2 bytes unsigned short al; // 2 bytes} ax_register;
Now, if you assign a value to ax_register. I, The 16bit value will be assigned to Ah, And the 16bit value will be assigned to Al. Is it very convenient?
For another example, if we use Union to assign an int value to an IP address, we can assign a value to the four segments of the IP address at the same time. If struct is used, it will be a lot of trouble, as follows:
union _ip_address { int i; // spaceholder unsigned char ip_first_num; unsigned char ip_second_num; unsigned char ip_third_num; unsigned char ip_fourth_num; } ip_address;
In this way, if you assign a value to ip_address. I, The 8bit is automatically assigned to four IP fields. Is it convenient? It is more convenient to compare the size of the two IP addresses. it is OK to compare the int value. The reason is that they share the same piece of memory. The disadvantage of Union is that the original data is easily erased by the data written later, so it is not suitable for data processing. It is generally used to store data.
Page147
Bit Field. I personally think it is used to save the information of a data. Here, we will compress and save the field for whether a data is a keyword, a static variable, or a global variable. That is, each identifier in the program has specific information associated with it, similar to the mask in the computer network.
struct {unsigned int is_keyword:1;unsigned int is_extern :1;unsigned int is_static:1;}flags;
Here, a variable flags is defined, which contains only three one-digit fields. Flags. is_exteern = 1; you can assign a value directly.
Chapter 7 input and output are not very useful for rough browsing
Summary: I have studied C language in my freshman year and C ++ in my sophomore year, so this book may not be as amazing as others have said, but I still learned something. In chapter 7, I have read a little bit about it. In chapter 8 and the appendix below, I have not done a few exercises, so I hope I can read them later.
Finally, a question was discussed in the lab today:
#include <stdio.h>int main(){const int a=4;int *b=(int *)(&a);printf("%d\n",&a);printf("%d\n",b);*b=10;printf("%d\n",a);printf("%d\n",*b);return 0;}
Description: A is a const type, B is a pointer to a address, first output the address, then change the value of B, and then enter the values of a and * B. According to common sense, * B and A indicate the same memory, so * B also changes a, but what is the result?
The output is as follows:
We can see that the value of a has not changed, which is related to the const type. After removing the const, the output is 10 and 10 according to common sense.
Why?
After the disassembly, the compiler finds that A is const and then passes 4 directly to printf without taking the value from the memory address. Therefore, the output is different. It seems that I learned something again. The discussion is a way to help people make progress.