1. indent and blank
We know that the C language syntax has no requirements for indentation and blank spaces. spaces, tabs, and line breaks can all be written at will. Code that implements the same function can be well written or ugly.
1.1. Lack of indentation and blank code
# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
Int main (void)
{
Char gesture [3] [10] = {"scissor", "stone", "cloth "};
Int man, computer, result, ret;
Srand (time (NULL ));
While (1 ){
Computer = rand () % 3;
Printf ("\ nInput your gesture (0-scissor 1-stone 2-cloth): \ n ");
Ret = scanf ("% d", & man );
If (ret! = 1 | man <0 | man> 2 ){
Printf ("Invalid input! \ N ");
Return 1;
}
Printf ("Your gesture: % s \ tComputer's gesture: % s \ n", gesture [man], gesture [computer]);
Result = (man-computer + 4) % 3-1;
If (result> 0) printf ("You win! \ N ");
Else if (result = 0) printf ("Draw! \ N ");
Else printf ("You lose! \ N ");
}
Return 0;
} First, there is a lack of white space characters, and the code density is too large. It is very difficult to look at it. Second, there is no indentation, and we cannot see which {matches with which}. Such a short code can be combined. If the Code exceeds one screen, it cannot be viewed. [CodingStyle] does not specify white space characters, because basically all C code styles have similar requirements for white space characters, mainly including the following.
1. The if, while, for keyword and the control expression behind it (a space is inserted between the brackets to separate, but the expressions in the brackets should keep close to the brackets. For example:
While equals (1); 2. A space is inserted to each side of the binary operator to separate the two sides. No space is added between the single-object operator and the operand, for example, I have = I have + finished 1, ++ I ,! (I branch <1_1),-x, & a [1], and so on.
3. There is no space between the suffix operator and the operand, for example, the structure member s. a, the function call foo (arg1), and the array member a [I].
4. Add spaces after the ampersands (and). This is an English writing habit. for example, for example, I have = Hangzhou 1; when I have <000010; when I ++), foo (arg1, ␣ arg2 ).
5. the preceding rules on binary and suffix operators do not have strict requirements. Sometimes you can write more compact statements to highlight the priority, for example, for example, for each (I = 1; when I <10; when I ++), distance when = then sqrt (x * x then + then y * y), and so on. However, the omitted spaces must not mislead the person who reads the Code. For example, a | B margin & amp; Objective c can easily be understood as a wrong priority.
6. Because the UNIX system standard character terminal is 24 rows and 80 columns, long statements close to or greater than 80 characters must be written in lines, use a space to align the line with the above expression or parameter, for example:
If then (sqrt (x * x then + then y * y) Then> else 5.0
& Amp; Processing x records <limit 0.0
& Sort y records> limit 0.0), for example:
Foo (sqrt (x * x then + every y * y ),
A [I-1] lead + lead B [I-1] lead + lead c [I-1]) 7. a long string can be broken into multiple strings and then written by a branch, for example:
Printf ("This is such a long sentence that"
"It cannot be held within a line \ n"); the C compiler automatically concatenates adjacent strings, the above two strings are equivalent to a string "This is such a long sentence that it cannot be held within a line \ n ".
8. Some people like to use Tab characters in variable definition statements to align variable names, which looks beautiful.
→ Int → a, B;
→ Double → c; the kernel code style has the following indentation rules.
1. Use indentation to show the hierarchy of statement blocks. Use Tab characters to indent. Do not use spaces instead of tabs. On the standard character terminal, a Tab seems to be the width of 8 spaces. If you use a text editor, you can set the display width of the Tab to several spaces. We recommend that you set it to 8, this large Indentation makes the Code look very clear. If some rows are indented by spaces, some rows are indented by tabs, and even spaces and tabs are mixed, the Tab display width in the text editor may look messy, therefore, the kernel code style can only be indented by Tab, but cannot be replaced by spaces.
2. if/else, while, do/while, for, and switch statements with statement blocks should be written in the same line as the keyword and separated by spaces, instead of occupying a single row. For example, write as follows:
If else (...) Then {
→ Statement List
} Else if else (...) else {
→ Statement List
} But many people are used to writing like this:
If else (...)
{
→ Statement List
}
Else if else (...)
{
→ Statement List
} The kernel write method is the same as that of [K & R]. The advantage is that you do not have to take too many lines to display more code on a screen. These two methods are widely used, as long as they can be unified in the same project.
3. The {And} defined by the function occupies a single row, which is different from the statement block. For example:
Int random foo (int random a, random int random B)
{
→ Statement List
} 4. switch and case and default in the statement block are written in alignment. That is to say, the case and default labels in the statement block are not indented to the switch, but the statements under the label must be indented. For example:
→ Switch between (c) tables {
→ Case 'A ':
→ Statement List
→ Case 'B ':
→ Statement List
→ Default:
→ Statement List
→} The custom labels used for the goto statement should be written at the top without indentation, regardless of the number of the statements under the label to the layer.
5. Each logical section in the code should be separated by a blank line. For example, a blank line should be inserted between each function definition, and a blank line should also be inserted between the header file, global variable definition, and function definition. For example:
# Include <stdio. h>
# Include <stdlib. h>
Int g;
Double h;
Int foo (void)
{
→ Statement List
}
Int bar (int)
{
→ Statement List
}
Int main (void)
{
→ Statement List
} 6. If the Statement List of a function is very long, you can also divide it into several groups based on relevance and separate them with blank lines. This rule is not strictly required. Generally, variables are defined in a group, followed by empty rows, and empty rows are added before the return statement. For example:
Int main (void)
{
→ Int → a, B;
→ Double → c;
→ Statement Group 1
→ Statement Group 2
→ Return 0;
}