C Coding Standard Recommendations
This is a preliminary attempt to come up with a set of C coding recommendations
for Upgrade Silicon Software developers. A meeting was held on 20 April 1995 to
come up with a set of standards. This is just a compilation of all the
recommendations. It should be noted that not all of the recommendations are
meant to be strictly followed. (Any statement with MUST is to be
strictly followed. Anything with SHOULD is strongly recommended)
Header Files
Every Header file must include:
#ifndef <FILENAME_H>
#define <FILENAME_H>
.
.
.
#endif
at the start with a name which corresponds to filename.h. For
example, if you have a file called types.h, the first two lines in that
file ignoring comments must be:
#ifndef TYPES_H
#define TYPES_H
.
.
.
#endif
Declarations
The "pointer" qualifier, '*', must be with the variable name rather
than with the type.
- char *s, *t, *u;
instead of
- char* s, t, u;
as C compilers may handle these assignments differently.
Functions
- Prototyping - Functions must be prototyped in a Header
file. Prototyping a function means to declare the function and all its
arguments types and return type in a Header file so that it can be made global
to all code.
- Naming Conventions - Function names should look as follows:
lllOoo...Aaa...(), where lll indicates the
library that the function is associated with, Ooo... is the
object the function acts on, and Aaa... is the action
performed on the object.
For example, in VxWorks you can probe the ASI memory address space for
bus errors using the routine vxMemProbe(). In this example,
vx is the library the function is associated with,
Mem is the object acted on by the function and
Probe is the action performed on the object
Mem.
- Internal functions in a module must be declared static.
- Input variables to functions which are not altered should be
passed with const type qualifier.
Code Formatting
- ALL code must be written in ANSI standard format.
- Names with leading and trailing underscores MUST NOT be used as
they are reserved for system purposes.
- #define constants must be in all CAPS.
- Should only write one statement per line.
Not Okay:
int x; int y; int z; for(x=0;x<200;x++){y=x; z=y*x; printf("%d\n",z);}
Okay:
int x, y, z;
for(x = 0; x < 200; x++) {
- y=x;
- z=y*x;
- printf("%d\n",z);
}
- Code should be indented, so that it is easier to read
For Example:
- if (x=1) {
- x=2;
- }
or
- if (x=1)
- {
- x=2;
- }
Macros
Refrain from using macros except when absolutely necessary. If used, the name
of the macro must clearly describe what it does. This will save a
great deal of time in searching through code to find what the macro does.
Send any comments or suggestions to
lpaterno@d0sf26.fnal.gov
Last Updated : 19-March-1996