Programming style of C language

Source: Internet
Author: User

C language Programming Style: first: Indent format

The tab is 8 characters, so the indentation is also 8 characters. There are a lot of weird styles, they define the indentation format as 4 characters (set to 2 characters!) Depth, which is as hard to accept as trying to define PI as 3.

The reason is that the size of the indentation is to clearly define the start and end of a block. Especially when you've been in front of the computer for more than more than 20 hours, you'll find that a large indentation format makes it easier to understand the program.

Now, some people say that using 8-character indentation makes the code close to the right, and the program is uncomfortable on a 80-character-wide terminal screen. The answer is, but your program has more than 3 indents, you should modify your program.

In summary, the 8-character indentation makes the program easy to read, and there is an added benefit of being able to warn you when you have too many layers of nesting. This time, you should modify your program.

Second: The position of the large symbol

Another problem with the C programming style is the handling of braces. Unlike indentation size, there is little reason to choose one without choosing another style, but there is a recommended style, which is brought by the book of Kernighan and Ritchie Classics, and it will begin

Brace is placed at the end of a line, and the closing brace is placed first on one line, as follows:

if (x is true) {We do y}

However, there is a special case: A named function: The opening parenthesis is the first bit on the next line, as follows:

int function (int x) {Body of function}

All non-Orthodox people will criticize this inconsistency, but all those who think normally understand: (first) K&r is ___ to ___, (second) if K&r is not correct, see article I. (:-))...... In addition, functions are special and do not necessarily have to be consistent.

It is important to note that the closing parenthesis is empty in the line it occupies, except that it follows the continuation symbol of the same statement. such as "while" in the Do-while loop, or "else" in the IF statement.

Do {body of Do-loop} while (condition);

And

if (x = = y) {:} else if (x > Y) {...} else {...}

Reason: K&r.

Also, note that the placement of this brace reduces the number of empty rows, but does not reduce readability. So, when the screen size is limited, you can have more empty lines to write some comments.

Third: Naming system

C is a concise language, so the name should be concise. Unlike MODULE-2 and Ascal languages, C programmers do not use naming methods such as Thisvariableisatemporarycounter. A c programmer will name it " TMP ", which is easy to write and not so difficult to understand.

However, descriptive names are necessary for global variables when the names of mixed types have to appear. Calling a global function called "foo" is annoying. Global variables (use it only if you have to use it), just like a global function. A descriptive naming method is required. If you have a function to calculate the number of active users, you should name it-"count_active_users ()"-or another similar form, you should not name "CNTUSR ()".

There is a name called Hungarian, which writes the type encoding of a function to the variable name, which is a symptom of a faulty brain---the compiler knows the type and checks it out, which only confuses the programmer. --Know why Micro$oft produced so many "bug" Programs!!.

The naming of local variables should be short and short. If you have a random integer loop counter, it may have "I", and if there is no possibility that it can be misunderstood, writing it "Loop_counter" is inefficient. Similarly, "TMP" can be a function variable of any temporary value.

If you are afraid to confuse the name of your local variable, there is another problem, which is to say

Function-growth-hormone-imbalancesyndrome.

IV: function

The function should be short and charming, and it will only do one thing. It should only cover a single screen of two screens (80*24 one screen), and only do a thing, and do it well. (This is not the Unix style, the translator notes).

The maximum length of a function is inversely proportional to the complexity of the function and the size of the indentation. So, if you've written a simple but long-length function, and you've done a lot of small things for different situations, it doesn't matter to write a longer function.

However, if you are going to write a complex function, and you have estimated that if the average person reads this function, he may not know what the function is saying, and this time, use a helpful function with a descriptive name.

Another thing to consider is the number of local variables. They should not be more than 5-10, or you might make a mistake. Reconsider this function and divide them into smaller functions. The human brain can often easily remember 7 different things, and exceeding this amount can cause confusion. You know you're smart, But you may still want to understand what you did 2 weeks ago. Job Coordinate www.zhizuobiao.com

V: Notes

Annotations are a good thing, but too many comments are dangerous, and don't try to explain how well your code is commented: You should write the code better, rather than spend a lot of time explaining the bad code.

Often, your comments are about what your code does, not how it is done. and try to avoid inserting annotations into a function body: If this function is really complex, you need to have some comments in it, You should go back to the fourth chapter. You can write short notes to indicate or warn about parts that you think are particularly smart (or extremely ugly), but you have to avoid too much. Instead, write the comment in front of the function, tell someone what it does, and maybe why.

Sixth: You are already in the midst of it.

Don't worry. You may have already been told that "GUN Emacs" will automatically help you with C's source code format, and you've seen it do, but, by default, it doesn't work as well (in fact, they're worse than the random knock-out thing-ainfinite number of monkeys typing into the GNU Emacs would never make a good program)

So, you can either not use the gun emacs or let it use sanervalules. Using the latter, you need to enter the following statements into your. emacs file. (Defun Linux-c-mode () "C mode with adjusted defaults for use with the Linux kernel." (interactive) (C-mode) (C-set-style "K&r") (setq c-basic-offset8))

This defines a command that m-x Linux-c-mode. When you hacking a module, how you will-*-linux-c-*-input in the first two lines, this mode will automatically work. And you might want to join the following

(Setq auto-mode-alist (cons ' ("/usr/src/linux.*/.*\\.〖ch〗$". Linux-c-mode) auto-mode-alist))

into your. emacs file, so that when you edit the file under/usr/src/linux, it automatically switches to Linux-c-mode.

But, if you can't get emaces to automatically handle the file format, don't be nervous, you have one thing: "Indent."

The GNU indentation format is also inflexible, which is why you need to add a few lines of command options. However, this is not too bad, because the GNU indentation format creators also remember the authority of K&R, (GNU is not guilty, they just wrong to guide people in this matter), you have to do is only the input option "- Kr-i8 "(means" k&r, indent 8 characters).

"Indent" has a lot of features, especially when it suggests you re-format your code, you should see Help. But remember: "Indentation" is not a panacea for poorly styled programs.

Programming style of C language

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.