C Language Coding Style (1)---->>linux C programming One-stop learning Song Jin Beijing Asian Embedded Education Research Center

Source: Internet
Author: User
Tags function definition

Indentation and whitespace Related: 1. Key WordsIf/while/for/switch/caseAfter the control-Made Expressions(Insert a space between the parentheses, but the expression inside the parentheses should be enclosed in parentheses.) For example:
 while (1

2. Insert a space between the two sides of the binocular operator, with no spaces between the monocular operator and the operand, for example

1),-X, &a[1]

such as

3, there is no space between the postfix operator and the operand, for example, the struct member S.A. , the function call foo (arg1) , and the array member A[i] .

4,,, and, after the number to add a space, which is the English writing habits, such as:

 for 1 ten; i++)               foo (arg1, arg2);

5, the above rules on binocular operators and postfix operators are not strictly required, sometimes in order to highlight the priority can also be written more compact, such as

 for (i=1; i<; i++)                  distance = sqrt (x*x + y*y);

such as But the omitted spaces must not mislead the person who reads the code, for example

a| | b && C

It's easy to make people understand the wrong priority.

6, because the UNIX system standard character terminal is 24 rows 80 columns, close to or greater than 80 characters of the longer statement to wrap the line, after the line with a space and the above expression or parameter alignment, for example:

if 5.0    0.0    0.0)

Another example:

Foo (sqrt (x*x + y*y),    a[i-1] + b[i-1] + c[i-1])

7. Longer strings can be broken into multiple strings and then written in branches, for example:

printf ("" "       It cannot be held within a line\n");

The C compiler will automatically connect multiple contiguous strings together, and the above two strings are equivalent to a string "this is such a long sentence The IT cannot be held within a line\n"
8, some people like to in the variable definition statement with the tab character, so that the variable name alignment, which looks very beautiful.

       →int    →a, b;       →double →c;

Kernel code Style There are several rules about indentation.
1, to use indentation to reflect the hierarchical relationship of the statement block, using the tab character indentation, you can not use a space instead of tab. In the standard character terminal the last tab appears to be 8 spaces wide, if your text editor can set tab display width is a few spaces, the suggestion is also set to 8, so that the large indentation so that the code looks very clear. If some lines are indented with a space, and some lines are indented with tabs, or even spaces and tabs are mixed, then when the tab width of the text editor changes, it will look very confusing, so the kernel code style rules can only be indented with tab, and cannot use spaces instead of tab.
2, If/else, while, Do/while, for, switch these statements that can take a statement block, the {or} of a statement block should be written on the same line as the keyword, separated by a space, rather than a single line. For example it should be written like this:

if  (...) {       elseif  (...) {       → statement list}

But many people are used to writing this:

if  (...) {       → statement list} Else if  (...) {       → statement list}

The kernel is written in the same way as [K&r], and the benefit is that it does not have to take up too many lines, allowing one screen to display more code. Both of these are widely used, as long as they can be unified in the same project.
3, the function definition of {and} a separate row, which is different from the provisions of the statement block, for example:

int foo (intint  b) {       → statement list}

4, switch and the case in the statement block, the default alignment, that is, the case in the statement block, the default label relative to the switch does not indent, but the label under the statement to indentation. For example:

      →switch  (c) {      →case'A':      →       → statement list      →  case'B':      →       → statement list      →default  :      →       → statement list      →}

The custom label used for the Goto statement should not indent the head, regardless of the number of words indented to the first layer.
5, each logical paragraph 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, the global variable definition, and the function definition, for example:

#include <stdio.h><stdlib.h>int  g; Double h; int foo (void) {       → statement list}int bar (int  a) {       → statement list} int Main (void) {       → statement list}

6, a function of the statement list if it is long, you can also be divided into groups according to the correlation, separated by a blank line. This rule is not strictly required, usually the definition of a variable to form a group, followed by a blank line, the return statement before the blank line, for example:

int Main (void) {       →int    →a, b;       →double  →c;       → statement Group 1       → statement Group 2       →return0;}

C Language Coding Style (1)---->>linux C programming One-stop learning Song Jin Beijing Asian Embedded Education Research Center

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.