C from zero (6) - What is said in front of what statement has been described in the process, and the description of the method is nothing more than the action of action, and the action here is in C through the statement, and The object of the action is also the resource capable of being operated, but it is 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. The operation mode of the machine code has said that all code in C to finally become a machine code that the CPU can know, and the machine code is an object of the action and action because it is the description of the method (or may not bring The object), that is, the logo of the machine command and the memory address or other hardware resources, and all of them are represented by a binary number. 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). The classification of statements is in C , with a total of six 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 Statement - // // // / ** / annihature, that is, the annotation of the interpretation, that is, some text information, what is the meaning of this code to see the source code, because the person's cognitive space and computer are complete Different, this will be specifically discussed in the future. 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 has explained that the source code (hereby referring to the code written in C ) sequentially converts the machine code represented by different lengths of binary numbers, and then placed in memory (this statement) Inaccurate). 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
It should be noted that the above deliberately let P1 and P2 definition, actually, ie,: long a = 1; p1: a = 1; p2: b * = a; goto p2; therefore look "P1:" and "P2:" is a separate definition statement. It should be noted that they should be the statement modifier, the role is to define the label, that is, the statement is wrong: long a = 1; p1: {a = 1; p2: b * = a; p3:} goto p2; top P3: The error will be reported because it is not modified any statement. It should also be noted that P1 is still 3005, ie "{}" is only just its composite effect, and does not actually generate code and does not affect the address of the statement. Judging the statement - IF ELSE, Switch if else said before, in order to achieve "what kind of situation", the C is very normally providing the conditional judgment statement to perform different conditions. Code. IF ELSE is: IF (
It should also be noted that since the composite statement is also a statement, IF (a) {long c = 0; C ;} This judgment statement is not ";", but it is still a single sentence, but it is still a single sentence, That is: if (a) IF (a <10) {long c = 0; C ;} else b * = a; Although it looks very complicated, it is still a single sentence, it should be noted when writing a "else" The compiler looks forward to a nearest "if" to match it, so "ELSE" is matched with "IF (a <10), not due to the above indentation and" if (a " "Match, therefore b * = a; only when A is greater than or equal to 10, not when A is zero. You should also pay attention to the IF (a) long C written in front ;. The meaning here is not if A is non-zero, defining the variable C, which involves the problem of the scope, will be explained in the following description. Switch's definition or less because of the cause of the implementation rather than the cause of logic as the "IF ELSE". Let's look at its format: Switch (
The above Break; statement is a specific, which is in the Switch's post-connected statement, indicating that the program jumps to Switch, for the above 3010 to execute B * = A; That is also: Switch (a) if (a) Break; since it jumps to the corresponding position, if A is -1, A will be executed; and then the A-; and then executes Break; jumps to 3010 Address execution b * = a; And, the above B = 0; will never be executed. Switch represents a value for a certain variable, which will result in different statements, which is ideal for implementation. For example, use 1 to say safety, 2 means a bit risk, 3 indicates a more dangerous, 4 means it is very dangerous, and you can determine the current status of a monster according to a monster's current state or "attack" or other one by writing a switch statement. Action to realize artificial intelligence in the game. 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
As follows: long A, b = 3; Switch (A 3) {Case 2: A ; Break; Case 3: A = 3; Break; Default: A -;} b * = a; above "Default: "Means that when A 3 is not 2 and less 3, then execute A-;, ie default represents the default situation, but may not, the Switch's statement will be executed directly, so this is: Switch (a) {} or switch (a); but only meaningless. Cycle statement - for, while, do while has just explained that the provision of the loop statement is mainly due to the short-on purpose, because the loop is used in the method description, and the algorithm is not complicated, and the development difficulty for the compiler is not Increase too much. FOR has formats for FOR (
Since the concept of statements and numbers in the previous repeated statements, it can be as follows: long A, b = 1; for (; b <100;) for (a = 1, b = 1; a; a, b) IF (b * = a) Switch (a = b) {case 1: a ; break; case 2: for (b = 10; b; b -) {a = b * b; case 3: a * = a;} Break;} looks very confusing, pay attention to "Case 3:" In the circulation body of a for statement after "Case 2:", that is, when A = B returns 1, jump to A ; At the time, the statement after the Switch is executed, that is, the statement after IF, is the second for statement A, B. When returns 2, the third For state is jumped to the third For statement, it will continue after the cycle is completed; and continue later. 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
The