./configure make make install
The formatting indicated below can be accomplished by
/usr/local/gnu/bin/indent -i2 -kr file.c expand file.c
Copyright (c) year by Columbia University. All rights reserved.where year is the current year. This applies to all projects done "for hire", i.e., by GRAs or staff members. Projects done by project students rather than GRAs should use something like
Copyright (c) year by Jane Doe. All rights reserved.
THIS FILE WAS GENERATED AUTOMATICALLY BY XXX. DO NOT EDIT!
pointers | prefix with p
pMemberlist
| |
In C, when you break functions out of the code you're calling, declare the broken-out functions static. This stops accidental namespace pollution. In C++, if you're breaking up member functions and the broken-out routines need access to data members, declare the broken-out routines as private member functions instead.
#ifdef __cplusplus extern "C" { #endif /* ... prototypes here ... */ #ifdef __cplusplus } #endif
if (condition1 && condition2)rather than
if ( condition1 && condition2 )The latter calls too much attention visually to the parentheses themselves, when it's the condition that's relevant.
One exception: it's acceptable to space-out the outermost parentheses if your parentheses are deeply nested, if it makes things visually clearer. (But if your parens are so deeply nested as to require this, you probably want to re-write your code to break up the expression anyway.)
(setq-default indent-tabs-mode nil)For vile, insert the following into .vilerc
unset tabinsert
if (x) { } switch (x) { case '1': break; case '2': break; }with spacing around () as shown for readability. For deeply nested control structures, it is helpful to include the nesting in a comment:
if (x) { while (y > 1) { for (i = 1; i < 10; i++) { } } /* while */ } /* if */
int main (int argc, char *argv[]) { return 0; } /* main */
There isn't a particularly rational reason for the style below, except that it seems to work and it's what a lot of the existing code does. It's a lot easier to read code if it's all formatted the same.
/* * message.c: manipulate messages and message queues. * Copyright 2001 by Columbia University; all rights reserved */
#include <stdio.h> /* printf(), gets() */
/* * AudioInput() * Receives audio from microphone input, performs silence detection (if * flag 'silence' is 1) and applies the codec 'codec'. Returns the * number of bytes read or -1 on error. */ int AudioInput(int silence, codec_t codec) { ... } /* AudioInput */
For C++ code, use the above style for extern "C" functions. For non-extern "C" functions, this style is ambiguous because C++ supports function overloading. Thus, in C++, use the following style:
/* * a_class::function_name(bar_t *bar, int baz) * Do foo to bar. If baz is true, also do quux. * Return false on failure. * XXX: also need to bletch. */ int a_class::function_name(bar_t *bar, int baz) { } /* a_class::function_name(bar_t *bar, int baz) */
/* Single line comments (space between * and word). */ /* * Multi-line comments, such as these. * And more. Note that the first and last line are empty. */
_t
convention, as in
struct queue { struct queue *next; int data; } queue_t;
if (p != NULL) if (strcmp(foo, bar) == 0)rather than
if (p) if (!strcmp(foo, bar))
There's no inefficiency with the former statements -- the compiler knows what it's doing -- and it's a lot clearer, when reading code, what you mean, and what the type of your variable is. (This one may be controversial. The 'if (p)' and '!strcmp' idioms are awfully common, but they obfuscate code without adding anything useful.)
Note, though, that if a function does return a boolean value, using it directly in a boolean expression is entirely appropriate, and explicitly testing it != 0 (or worse, explicitly testing it == 1) is a bad idea.
uint8_t | 8-bit unsigned integer |
uint16_t | 16-bit unsigned integer |
uint32_t | 32-bit unsigned integer |
int32_t | 32-bit signed integer |
If available, use the standard header files <stdint.h> or <inttypes.h>.
Usage: cat [OPTION] [FILE]... Concatenate FILE(s), or standard input, to standard output. -A, --show-all equivalent to -vET -b, --number-nonblank number nonblank output lines -e equivalent to -vE -E, --show-ends display $ at end of each line -n, --number number all output lines -s, --squeeze-blank never more than one single blank line -t equivalent to -vT -T, --show-tabs display TAB characters as ^I -u (ignored) -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB --help display this help and exit --version output version information and exit With no FILE, or when FILE is -, read standard input.The usage of longer parameter (with double-dashes) is optional and needed only if you have lots of parameters.
if ((s = foo(y,z)) < 0) { perror("foo"); /* actually, this should be error function */ exit(1); }
Robust programming, also called bomb-proof programming, is a style of programming that prevents abnormal termination or unexpected actions. Basically, it requires code to handle bad (invalid or absurd) inputs in a reasonable way. If an internal error occurs, the program or library terminates gracefully, and provides enough information so the programmer can debug the program or routine.