Coding habits and coding norms play an important role in programming. Good coding habits help reduce Code The error rate also facilitates code debugging. The standard encoding enhances the readability of the code and makes code communication possible. This article will introduce the paired encoding method in detail, and keep the standard encoding in the next article.
The basic idea of paired encoding is extremely simple, that is, "paired ". Pairing is extremely common in C. The function bodies of any function are enclosed by pairs. From this point, we can give the basic method of paired encoding. Below we will write a "hello World"ProgramThis method is described as an example.
Encoding Step 1:
Int main (void)
{
}
Enter a "{" and press enter to immediately create "}".
Step 2 of encoding:
Int main (void)
{
Printf ("Hello world! ");
Getch ();
Return 0;
}
Move the cursor to the end of the previous line and press the tab key to insert code between the brackets.
Note that, in the first step, we give the brackets of the function in pairs. If we create a "{", we should not hesitate to give "}", this is the paired encoding. In step 2, we move the cursor between two braces and insert code in the middle. This point is extremely important. We completely discard the Backward Linear Encoding (that is, the write-down of one character and one character), but rather the Skip nonlinear encoding. This method is superb. At any step of coding, we can ensure that the code can be compiled, run, and debugged. At least it can ensure that there will be no compilation errors with less than one or more.
This basic method can also be applied to any statement that uses "{}", such as if and for. For example, the following code:
Step 1:
int main ()
{< br> int I;
for (I = 0; I <10; I ++)
{< BR >}
Step 2:
int main ()
{< br> int I;
for (I = 0; I <10; I ++)
{< br> printf ("paired encoding is a good method! /N ");
}< BR >}
Step 3:
int main ()
{< br> int I;
for (I = 0; I <10; I ++)
{< br> printf ("paired encoding is a good method! /N ");
}< br> getch ();
return 0;
}
Another promotion of paired encoding is that malloc () and free () can also be considered as paired. In encoding, if we write a malloc (); statement, we should not hesitate to write a free (); statement. The rest is to insert code between clon statements, in this way, the memory leakage will not occur.
Paired encoding can also be promoted to any statement that is paired with malloc () and free. For example, when using a linked list, I usually write the initlist () and destroylist () Statements in pairs, and then insert the operations on the linked list into the two sentences.
We will soon be able to understand the superiority of paired encoding. Compared with Linear Encoding, paired encoding will hardly produce code compilation errors, and the code can be run and debugged at any time. I have used the paired encoding method in all the programs I have compiled, and there are almost no compilation errors, which is really nice.
Currently, many C language textbooks only focus on grammar and other content, but almost do not mention the encoding method. When we read the code, we also get used to linear reading from top to bottom, linear coding habits have been developed. As an efficient coding method, paired encoding must be carefully studied and mastered. At the same time, you can also use the pair-based reading method to read and understand the code and the city.