C ++ starts from zero (six) - what is phrase

zhaozj2021-02-12  138

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 () ELSE or IF () Long A = 0, B = 1; P1: A ; B * = A; IF (a <10) goto p1; long c = b; the above code means that only the value of A is less than 10, only jumps to P1 to repeat it, the final effect is a step of the value of C. The above indicates a number in parentheses after "if", ie, the expression, and when the value of this number is not zero, that is, the logic is true, the program jump to perform , if zero , Ie logic fake, execute . That is, the same: IF (a - 10) goto p1;, it means that GOTO P1 is only performed when A - 10 is not zero ;. This is the same as the previous effect, although the final C is still 10 steps, but the meaning is different, the readability of the code decreases, unless it is not recommended to write code. and Due to the statement, you can put any statements, so it can be like this: if (a) long c; above, it can be presumed, here is just to illustrate < Statement 1> Actually, anything is something, but since the definition of the label is not the statement, the indication statement and the precompiled statement are actually the statement, so the following is a non-zero, defined the reference number P2 and X Zero, write a comment "Error!" The intent is incorrect: if (a) p2: or if (! A) // error! A ; A ; but the compiler will not report an error, because the former actually increases A to a non-zero; the latter actually increases A when A is zero.

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 and the IF statement, as long as it is a number, but different places must be an integer number (later description reasons). Then thereafter The same as before, as long as it is a statement. In , this should be used: Case :. It defines a label in the position it corresponding to it, that is, the front GOTO statement is used, indicating if and are equal, the program jumps to "Case : "The location identified, otherwise the subsequent statement will be performed. Long A, B = 3; Switch (A 3) Case 2: Case 3: A ; B * = A; The above means that if A 3 is equal to 2 or 3, it jumps to the A ; then executes A , Otherwise, the following statement b * = a; This looks ridiculous, what is the use? A statement is of course meaningless, in order to identify multiple statements, you must use a composite statement, that is, the following: long A, b = 3; switch (A 3) {b = 0; case 2: A ; // hypothesis address 3003 Case 3: a-; // Assume the address is 3004 Break; case 1: a * = a; // Assume the address is 3006} b * = a; // Assume the address 3010 should pay attention to "2:", "3:", "1:" It is a full number, but it should actually understand them as a label. Therefore, the value of A 3 is checked above. If equals 1, jump to the address of "1:" identifier, that is, 3006; if it is 2, jump to 3003, if it is 3, jump to 3004 The location continues.

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 , because these numbers will be used for the subscript or offset of arrays, so it must be an integer. must be constant because it is told by compiler. It is now in the first few elements of the address array. After you understand the implementation of Switch, you should use the Switch of the Switch or its multiple a factor thereof to reduce the size of the array that needs to be generated without the size of the array that needs to be generated without the size of the array that needs to be generated. That is, Case 1000, Case1001, Case 1002, and Case 2, Case 4, Case 6 only uses an array of magnifications, Case 0, CASE 100, and Case 101 requires 102 elements of an array. It should be noted that the current compiler is very intelligent. When the latter is found to have only 3 branches of the latter to 102 elements, the compiler is possible to use a duplicate if statement to replace the generation of the above array. . Switch also provides a keyword - DEFAULT.

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 (; ; ) . The is the same, that is, the plurality of words can also be connected to the composite statement. , and Because it is a number, it is an expression, and there is a calculation of all work-operators that can be made by expression statements. The FOR statement means that is calculated first, equivalent to initialization, and then calculates . If the value of is zero, representing logic, then exits the loop, execute the statement behind, otherwise , then calculate the "routine, the routine, the routine, then calculate and repeat. The above is generally referred to as a cyclic body. The above design is a process-oriented design idea, regarding the cyclic body as a process, the initialization of this process () and must be executed (). A simple cycle, as follows: long A, b; for (a = 1, b = 1; a <= 10; A ) b * = a; the above execution B is the step of 10, and the IF statement Compared with the example, it is more simple, and readability is better --a = 1, b = 1 is the initialization operation, each loop will add A, which is a goto and if statement. from.

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 may not be a number, but a variable definition statement, that is, this: for (long (long a = 1, b = 1; a <10; ) A, b) ;. Among them, variables A and B are defined. However, only the variable definition statement can be connected, and the structure definition, class definition, and function definition statement will not be written here. This syntax proposes to further define the for statement as a process of a count cycle, and the variable definition statement is used to define a variable (above A) that acts as a counter in this loop to implement the number of cyclic fixations. Finally, you should also pay attention to the , and above, you can: for (;;) ;. While its format is while () , the and are all in the same, meaning it is obvious, when non-zero, execute , otherwise execute the statement behind while, The is referred to herein as a cyclic body. Do WHILE has a format of Do While () ;. Note that after the while, ";"; "to indicate the end of this single sentence.

The and are all in the same, it means that when non-zero, is executed, otherwise the statement behind the While is executed, and the is referred to as a cyclic body. Why is C to provide three cyclic statements above? Demolition is an important purpose, but more importantly, you can provide some optimization. For designed cycles for fixed number of times, while While and Do WHILE are cycles for conditions determined. For the former, the compiler can map the previously mentioned variables into registers to optimize speed, while the latter will determine whether to generate optimized code as the intelligence of the compiler. The main difference between While and Do While is that the former's cyclic body is not necessarily executed, and the latter's cyclic body must be at least once. For the dealer, C has also proposed a Continue and Break statement. As follows: for (long i = 0; i <10; i ) {if (! (I% 3)) Continue; if (! (@)) BREAK; // Other statement} The value of i is 3 When the entire is removed, the "other statement" later is not executed, but directly calculates i , then calculates I <10 to determine whether to continue cycle. That is, Continue is terminated the execution of the current cycle and starts the next loop. The upper "other statement" is not executed when the value of i is 7, but the loop body is not executed, but the cycler is jumped out, and the FOR will execute the statement. That is, BREAK is the operation of the termination cycle, and immediately jumps out of the cyclic body. 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.

转载请注明原文地址:https://www.9cbs.com/read-6566.html

New Post(0)