C starts from scratch (6)
What is the statement?
As mentioned earlier is the description of the method, and the method of the method is nothing more than the action of action, and the action here is manifested by the statement, and the object of the action is the resource capable of being able to operate. But unfortunately C language itself only supports a resource-memory. Since the computer can actually operate more than memory this kind of resource, causing the C language to actually be written in the underlying hardware program (even if C language is not possible), each compiler manufacturer provides its own embedded compilation statement function ( Other additional syntax may also not be provided or other additional syntax so that the hardware can be operated. For VC, the assembly code is added to the C code to operate other types of hardware resources by using the __asm statement. This series does not explain this statement. The statement is the action, and there are two statements in C : a single sentence and a composite statement. The composite statement is enclosed in a pair of braces to place multiple words while needing, such as: {long a = 10; A = 34;}. The single sentence is ended in ";", but it may also be used to end with the "}" by a composite statement in the end of the place to insert a single sentence, such as: if (a) {a-; a ;}. It should be noted that there is no need to write ";" because it is not a single sentence. How is the method of doing, and what kind of action is to do in what kind of action? Because the resources that can be operated in C are only memory, the action is very simple, just about the calculation and assignment of memory content, which is the expression that said in the previous. And "What order", C forcibly ruled that only from the top, from left to right to perform a single sentence or compound statement (do not confine the calculation order about the expression, then in a single sentence rule). And the last for "What is the case", that is, the judgment of conditions. For different cases of different code, C defines the jump statement to implement, which is implemented based on the CPU running rules. Let's first see how the CPU performs machine code.
Machine code
As mentioned earlier, all code in C to finally become machine code that CPUs can know, and machine code is an object of action and action (also without an object), ie machine The instructions and memory addresses or other hardware resources, and all are represented by binary numbers. Normally, these representative machine code's binary number of efficiency should be placed in memory during execution (actually in hard disk or other storage device), it is normal to have an address for each machine instruction. And it corresponds to it. The CPU has a function and memory hardware for temporarily recording the binary number, called a register, and its read speed is much faster than the memory, but the size is much smaller. In order to speed up the read speed, the register is removed from the addressing circuit and one register can only store one 32-bit binary number (for 32-bit computers). The CPU uses one of the registers to record the location of the machine instruction you are currently running, called it as an instruction register. When the CPU runs, take the value of the command register, and then find the corresponding memory, read the contents of one byte, see what the machine instructions corresponding to this 8-bit binary number, and make the corresponding action. Due to different instructions, there may be different quantities of parameters (i.e., the objects of the previous action), such as multiplication commands to multiply them to multiply them, while refrigeration operations require only one parameter. And the multiplication of the two 8-bit binary numbers and two 16-bit binary numbers are also different, so the length of the machine code formed by different instructions can be different. After each CPU performs a machine code, add the contents of the command register with the length of this machine code to make the command register point to the next machine code, and repeat the above process to implement the operation of the program (this is simply explained. Due to the addition of various technologies, such as high-speed buffering, the actual operation process is much more complicated than this). Classification of statements
In C , there are 6 species: declarative statements, definition statements, express statements, instruction statements, pre-compiled statements, and comment statements. The following statement statements, the pre-encoding statement will be described in "C from zero (sixteen)", and the definition statement is the definition variable that has been seen in front, and will also explain the definition function, structure, etc. The expression statement is an expression directly to ";", such as: 34;, A = 34;, etc., to generate the corresponding code for the memory value by defining the calculation function of the operator. The comment statement is to comment the statement of the code, that is, written to people, not to see the compiler. The final instruction statement is the statement that contains the keywords described below, that is, their use is not operating memory, but to achieve the "what kind of situation" as mentioned earlier. The declaration statement here, the pre-compiled statement, and the comment statement will not be converted into machine code, ie the three statements are not to operate the computer, but other uses will be detailed later. The definition statement does not necessarily generate machine code, only the expression statement and the instruction statement will generate code (regardless of the optimization function of the compiler). It should also be noted that you can write a speech statement, that is,; or {}, they do not generate any code, and its effect is only to ensure the correctness of the syntax, and will be seen later. The following describes the comment statements and instruction statements - jump statements, determines statements, and loop statements (actually not thus increasing some statements due to the introduction of exceptions and template technology, will be described in explanation of abnormalities and templates).
Note Strategy - /// / ** /
Note, that is, the label for explanation, that is, some text information, what is the meaning of this code to see the source code, because people's cognitive spaces and computers are completely different, this will explain how to program how to program discuss. To write a paragraph to comment, use "/ *" and "* /" to enclose this paragraph, as follows: long a = 1; a = 1; / * a is a person's number, people The number plus one * / b * = a; / * B is a per capita cost, get the total cost * / above, respectively, for A = 1; and b * = a; write two annotation statements to explain The respective semantics (because as long as C knows that they are a variable from the self-increasing one and another variable, but do not know the meaning). The above trouble is to write "/ *" and "* /", a bit trouble, so C provides another annotation statement - "//": long a = 1; A = 1; // A is the number of people, and the number of people plus one b * = a; // B is put on a per capita cost, get the total cost above and the previous equivalent, where "//" said from it All characters behind this line see a comment, the compiler will not pay attention, "即 = 1; A = 1; // a is the number of people, let the number plus one b * = a; where B * = a; will not be compiled, because the previous "//" has already told the compiler, starting from "//", all characters behind this line are comments, so the compiler will not Compile B * = a ;. But if long a = 1; A = 1; / * a is a person's number, let people add one * / b * = a; this compiler is still compiled B * = A; "/ *" And "* /" are included. It should be noted that the comment statement is not a statement, which is not ";", it is just another syntax to provide a comment function, just like the precompiled statement to be explained later, it is not a statement, not ";" Neither a single sentence is not a composite statement, but it is still called statements for habits. Jump Statement - Goto
As mentioned earlier, the source code (hereby referring to the code written in C ) sequentially converts the machine code represented by the length of the binary number of lengths, and then placed in memory (this statement is not accurate). Such a code as follows: long a = 1; // Assume that the length is 5 bytes, the address is 3000 A = 1; // The address is 3005, the assumption length is 4 bytes B * = a; // Its address is 3009, assuming that 3000, 3005, and 3009 above the length of 6 bytes represent the position of the three statements in memory, and the so-called jump statement, that is, the address of the above 3000, 3005 and the like In the instruction registers of the previously enumerated, the CPU begins to execute from a given location to exhibit a change in execution order. Therefore, there must be a means to express the address of the statement, and C gives this label. Write an identifier, then ":" to establish a mapping, bind this identifier and the address of its location, as follows: long a = 1; // Assume the length of 5 bytes, address is 3000p1 : A = 1; // The address is 3005, assuming the length of 4 bytes P2: b * = a; //, its address is 3009, assuming the length of 6 bytes GTO P2; the top P1 and P2 are Number, its value is 3005 and 3009, and the last Goto is a jump statement, its format is goto
In front of IF Else, in order to achieve "what kind of situation", "What kind of action" is made, the C provides a conditional judgment statement to perform different code in realization. IF ELSE is: IF (
Let's look at its format: Switch (
Isn't it very strange? The above Switch can also be implemented through the IF statement, why should I provide a Switch statement? If it is just to be short-handed, why is it not to provide more short-term similarly similar to this logic, but only provide a branch selection and a short-term loop? Because it is made for an optimization technology, it is as follows of the recirculating statement, and their contribution to logic can be implemented through the IF statement (after all logic is judging), and their proposed a certain degree is based on Some kinds of optimization techniques, but the subsequent recirculating phrase is more important. We give an array, each element of the array is 4 bytes, then for the above Switch statement, as follows: unsigned long addr [3]; addr [0] = 3006; addr [1] = 3003; addr [2] = 3004; For Switch (A 3), use similar statements to replace: goto addr [A 3 - 1]; the above is the true face of Switch, pay attention to the above GOTO is wrong This is why there will be Switch statements. The compiler builds a store address for the array of storage addresses. Each element of this array is an address that represents the address of a statement, so that the jump to different locations can be implemented by different offsets. Different statements in turn exhibit a choice of status. It should now be understood why it must be
It has just been explained that the provision of the cycle statement is mainly due to the shortest purposes, because the loop is the most used in the method description, and the algorithm is not complicated, and the development difficulty to the compiler is not increased. FOR has formats for FOR (
When returning 3, jump to a * = a; then execute, then calculate B-, then calculate the value of B, check if it is non-zero, then repeat the cycle until B is zero, and then proceed. The above code is not meaningful. Here is to write such a confusion to further illustrate the concept of the statements and numbers mentioned earlier. If you really implement it, it is easy to know that you will be a dead cycle, that is, you will not exit. cycle. It should also be noted that C proposes a special syntax, that is, the above
As follows: while (--i) do {{if (i == 10) IF (i == 10) Continue; Continue; if (i> 20) IF (i> 20) BREAK; BREAK; / / Other statements / / Other statements}} while (--I); a = i; a = i; the above Continue; execution is immediately calculated to determine whether to continue cycle, and Break; exits the cyclic body immediately Perform a subsequent A = I; It should also be paid attention to nested problems, that is, the previous ELSE is looking for a paired IF, always looking for a nearest IF, which is still. Long a = 0; p1: for (long i = a; i <10; i ) for (long j = 0; j <10; j ) {if (! (j% 3)) Continue; if (! (j % 7)) BREAK; if (i * j) {a = i * j; goto p1;} // Other statement} The topue on the above; after execution, J will be immediately calculated, and Break; execution, will exit the second A loop (ie J cycle), in turn executes i , and then proceeds to I <10 to determine whether to continue cycle. When Goto P1; execution, the program jumps to the upper P1, that is, executes long i = a; and then restarts the cycle. It is not recommended to write a goto statement above because it destroys the cycle, and does not conform to human thinking habits. Here is just to indicate, for or while, do while, etc. is not a loop, just their respective uses, finally express it seems to be a loop, actually only the change of the program execution location. It should be clearly realized, so that the actual role of various statements can be clearly understood, and it is clear that the code of the code is written. For yourself writing code, understanding the implementation of the statement will help make certain optimization. But when you write a program that streamlines high efficiency, keeping its good readability is a programmer's literacy, and we should try to develop the habit of writing the readability of readability. The upper LONG J = 0 in the first loop cycle body, isn't it necessary to define multiple times? This is a problem with the scope of the variable, which will be explained.