The programming style of C language

Source: Internet
Author: User
Tags empty numeric value

Indent format

tab is 8 characters, and the indentation is 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: the size of the indentation is to clearly define the beginning and end of a block. Especially if you've been in front of the computer for more than more than 20 hours, you'll find that a large indentation format makes your understanding of the program easier.

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

All in all, the 8-character indentation makes the program readable, and there's an added benefit that it warns you when you have too many layers of nesting. This time, you should revise your program.

The position of the large symbol

Another problem with the programming style of C programs is the handling of curly braces. Unlike indented sizes, there is little reason to choose one rather than another, but there is a recommended style, which is brought by Kernighan and Ritchie's classic book, which puts the opening braces at the end of a line , and put the closing brace on the first digit of the line, as follows:

if (x is true) {We do y}

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

int function (int x) {Body of function}

All unorthodox people would accuse of this inconsistency, but all normal people understand that (first) K&r is ___ to ___, (second) if K&r is not correct, see article One. (:-))...... In addition, functions are special and do not necessarily have to be consistent.

Note that the closing bracket is empty in the line it occupies, except __ It follows the continuation symbol of the same statement. such as "while" in the Do-while loop, or "else" in the IF statement. as follows:

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

And

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

Reason: K&r.

Also, note that the placement method 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 blank lines to write notes.

Naming system

C is a concise language, so the name should also be concise. Unlike MODULE-2 and Ascal languages, C programmers do not use naming methods such as Thisvariableisatemporarycounter. A C-language 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 name of the mixed type is forced to appear. It's annoying to call a function called "foo" global. Global variables (used only when you have to use them), like global functions, Descriptive naming is required. If you have a function that calculates the number of active users, you should name it--"count_active_users ()"--or another similar form, which you should not name "CNTUSR ()".

There is a method called Hungarian naming, which writes the type encoding of a function to a variable name, which is a manifestation of a brain problem---the compiler knows this type and will check it, which will only confuse the programmer. --Know why Micro$oft produces so many "bug" programs to put!!.

Local variables should be named Dapper. If you have a random integer loop counter, it may have "I", and it is inefficient to write "loop_counter" if there is nothing that might make it possible to be misunderstood. Similarly, "TMP" can be a function variable for any temporary numeric value.

If you are afraid to confuse the name of your local variable, there is another problem, which is to call Function-growth-hormone-imbalancesyndrome.

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.