Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
GNU coding standards
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{More footnotes|date=September 2009}} {{about||the quantum error correcting code|gnu code}} The '''GNU coding standards''' are a set of rules and guidelines for writing [[computer program|program]]s that work consistently within the [[GNU]] system. The GNU Coding Standards were written by [[Richard Stallman]] and other GNU Project volunteers. The standards document is part of the [[GNU Project]] and is available from the GNU website. Though it focuses on writing [[free software]] for GNU in [[C (programming language)|C]], much of it can be applied more generally. In particular, the GNU Project encourages its contributors to always try to follow the standards—whether or not their programs are implemented in C. ==Code formatting== The GNU Coding Standards specify exactly how to format most [[C (programming language)|C programming language]] constructs. Here is a characteristic example: <syntaxhighlight lang="c"> int main (int argc, char *argv[]) { struct gizmo foo; fetch_gizmo (&foo, argv[1]); check: if (foo.type == MOOMIN) puts ("It's a moomin."); else if (foo.bar < GIZMO_SNUFKIN_THRESHOLD / 2 || (strcmp (foo.class_name, "snufkin") == 0) && foo.bar < GIZMO_SNUFKIN_THRESHOLD) puts ("It's a snufkin."); else { char *barney; /* Pointer to the first character after the last slash in the file name. */ int wilma; /* Approximate size of the universe. */ int fred; /* Max value of the `bar' field. */ do { frobnicate (&foo, GIZMO_SNUFKIN_THRESHOLD, &barney, &wilma, &fred); twiddle (&foo, barney, wilma + fred); } while (foo.bar >= GIZMO_SNUFKIN_THRESHOLD); store_size (wilma); goto check; } return 0; } </syntaxhighlight> The consistent treatment of blocks as statements (for the purpose of indentation) is a very distinctive feature of the GNU C code formatting style; as is the mandatory space before parentheses. All code formatted in the GNU style has the property that each closing brace, bracket or parenthesis appears ''to the right'' of its corresponding opening delimiter, or in the same column. Being tightly integrated with the GNU system, [[GNU Emacs]] provides automatic formatting of C code to match the GNU coding standards.<ref>{{cite web |url=https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#index-GNU-style|title=CC Mode Manual|access-date=2025-04-08}}</ref> Rather than manually modifying code formatting in a way that strays from the GNU coding standards, the formatted layout of the code can be tweaked by writing it in a more Emacs-friendly form—for example, by inserting additional parentheses. ===Splitting long lines=== "When you split an expression into multiple lines, split it before an operator, not after one."<ref>{{Cite web|url=https://www.gnu.org/prep/standards/standards.html#Formatting|title=GNU Coding Standards|website=www.gnu.org|language=en|access-date=2020-11-29}}</ref> For example: <syntaxhighlight lang="c"> if (foo_this_is_long && bar > win (x, y, z) && remaining_condition) </syntaxhighlight> ==Comments== The standards greatly emphasise the importance of [[English-language]] [[Comment (computer programming)|comments]]: <blockquote>Please write the comments in a GNU program in English, because English is the one language that nearly all programmers in all countries can read. If you do not write English well, please write comments in English as well as you can, then ask other people to help rewrite them. If you can't write comments in English, please find someone to work with you and translate your comments into English.</blockquote> Comments should consist of complete, capitalized sentences, each followed by two spaces (so that Emacs can tell where one sentence ends and the next begins). For long or complex preprocessor conditionals, every <code>#else</code> and <code>#endif</code> should have a comment explaining the condition for the code below (for <code>#else</code>) or above (for <code>#endif</code>). ==Files== The standards require that all programs be able to operate when <code>/usr</code> and <code>/etc</code> are [[mount (computing)|mounted]] read-only. Therefore, files that are modified for internal purposes (log files, lock files, temporary files, etc.) should not be stored in either <code>/usr</code> or <code>/etc</code>. An exception is made for programs whose job it is to update system configuration files in <code>/etc</code>. Another exception is made for storing files in a directory when the user has explicitly asked to modify a file in the same directory. ==Portability== The GNU Coding Standards define the issue of portability in this way: portability in the [[Unix]] world means 'between Unixes'; in a GNU program this kind of portability is desirable, but not vitally important. According to the standard, portability problems are very limited as GNU programs are designed to be compiled with one compiler, the [[GNU Compiler Collection|GNU C Compiler]], and only run on one system, which is the GNU system. There is one form of portability problem though, and that is the fact that the standard makes it clear that a program should run on different [[Central processing unit|CPU]] types. The standard says that GNU doesn't and won't support 16-bit systems, but handling all the different 32- and 64-bit systems is absolutely necessary. == Criticism == The GNU coding standards are primarily used by GNU projects, though its use is not limited to GNU projects alone. The [[Linux kernel]] strongly discourages this style for kernel code, and refers to the style pejoratively: "First off, I’d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it’s a great symbolic gesture.".<ref>{{Cite web|url=https://www.kernel.org/doc/html/v4.10/process/coding-style.html|title=Linux kernel coding style — The Linux Kernel documentation|website=www.kernel.org|language=en|access-date=2017-10-12}}</ref> [[Steve McConnell]], in his book [[Code Complete]], also advises against using this style; he marks a code sample which uses it with a "Coding Horror" icon, symbolizing especially dangerous code, and states that it impedes readability by requiring an extra level of indentation for braces.<ref>{{cite book |title=Code Complete: A practical handbook of software construction |last=McConnell |first=Steve |publisher=Microsoft Press |year=2004 |isbn=0-7356-1967-0 |location=Redmond, WA |pages=[https://archive.org/details/codecomplete0000mcco/page/746 746–747] |author-link=Steve McConnell |url=https://archive.org/details/codecomplete0000mcco/page/746 }}</ref> == See also == {{Portal|Free and open-source software}} *[[Gnits standards]] ==References== {{Reflist}} ==External links== * [https://www.gnu.org/prep/standards/ The GNU Coding Standards] on the GNU website * [https://web.archive.org/web/20090304122402/http://www.ronancrowley.com/Eclipse_GNU_Style.xml Eclipse Code Style Formatter for GNU Coding Standards] {{DEFAULTSORT:Gnu Coding Standards}} <!-- Categories --> [[Category:Free software culture and documents]] [[Category:GNU Project|Coding Standards]] [[Category:Source code]] [[Category:Articles with example C code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:More footnotes
(
edit
)
Template:Portal
(
edit
)
Template:Reflist
(
edit
)