C/C++ Programming Style Guidelines



The formatting style presented here is essentially that used by Stroustrup in The C++
Programming Language
. If you use Emacs you can make this your default editing
mode by adding the following to your

.emacs
file:
(defun my-c-mode-common-hook ()
(c-set-style "stroustrup"))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
Format your code so that the spatial structure illustrates the logical structure. Use blank
lines to help separate different ideas, use indentation to show logical relationships, and
use spaces to separate functionality. Each block of code should do exactly one thing.

Start all function definitions and declarations in column zero. Put the return value type,
the function interface signature (name and argument list), and the function body open
bracket each on a separate line. For functions that are more than a few lines long, put
the function name after the closing bracket in a comment.

Example 3. Formatting function declarations and definitions
void
debug(const string& message);
int
Class::method(const int x, const string& str)
{
.
.
.
} // method