Why Use It

Formatting Show




FAQs & Tips

Update History

Award Gallery


License Policy



GNU Coding Style

SourceFormatX Predefined Coding Style Schemes - GNU Coding Standards:

The GNU coding standards are a set of rules and guidelines for writing programs that work consistently within the GNU system. 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, 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. The C code formatting style is well-known within the free software community, but of course anyone can choose to follow it.

The GNU Coding Standards were written by Richard Stallman and other GNU project volunteers. The GNU coding standards specify exactly how to format most C programming language constructs. Here is a characteristic example:

   main (int argc, char *argv[])
     struct gizmo foo;

     fetch_gizmo (&foo, argv[1]);

     if (foo.type == MOOMIN)
       puts ("It's a moomin.");
              || (strcmp (foo.class_name, "snufkin") == 0)
                  && < GIZMO_SNUFKIN_THRESHOLD / 2)
       puts ("It's a snufkin.");
         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.  */

             frobnicate (&foo, GIZMO_SNUFKIN_THRESHOLD,
                         &barney, &wilma, &fred);
             twiddle (&foo, barney, wilma + fred);
         while ( >= GIZMO_SNUFKIN_THRESHOLD);

         store_size (wilma);

         goto check;

     return 0;

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.

As a general principle, GNU Emacs can be considered a reliable authority on the GNU code formatting style. As such, it is desirable that any piece of code that looks ugly when indented by Emacs is changed into a more Emacs-friendly form for example, by inserting additional parentheses.

The standards greatly emphasise the importance of English language comments: 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.

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 #else and #endif should have a comment explaining the condition for the code below (for #else) or above (for #endif).

The standards require that all programs be able to operate when /usr and /etc are mounted read-only. Therefore, files that are modified for internal purposes (log files, lock files, temporary files, etc.) should not be stored in either /usr or /etc. An exception is made for programs whose job it is to update system configuration files in /etc. Another exception is made for storing files in a directory when the user has explicitly asked to modify a file in the same directory.

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 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 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.

What's GNU?

Self-referentially, short for GNU's not UNIX, a UNIX-compatible software system developed by the Free Software Foundation (FSF). The philosophy behind GNU is to produce software that is non-proprietary. Anyone can download, modify and redistribute GNU software. The only restriction is that they cannot limit further redistribution. The GNU project was started in 1983 by Richard Stallman at the Massachusetts Institute of Technology.

Linux systems rely heavily on GNU software and in the past, GNU systems used the Linux kernel. This close connection has led some people to mistakenly equate GNU with Linux. They are actually quite separate. In fact, the FSF is developing a new kernel called HURD to replace the Linux kernel in GNU systems.

Coding style guides, coding standard, code conventions, code guidelines, programming manuals and coding references are correlative topics of coding standards and programming style.