Guidelines For C: Variables and Data: Rules For Variables
Guidelines For C: Variables and Data: Rules For Variables
Guidelines for C
Following are the coding standards for C programming language. C coding standards will be
described under the following sections:
• Variables and Data
• Operators
• Functions
• General guidelines
Choosing Names:
• Meaningful names for variables, constants and functions should be chosen.
• All the abbreviations used for a project should be stored in a file, which is accessible to all
team members.
• Variables of all classes should be suffixed with an underscore, followed by one, two or
three character; signifying its class as given below:
Major suffixes
o Global variables: '_g'
o Local variables: '_l'
o File variables: '_f'
e.g.
long lowerLimit_g; /* Minimum value */
long upperLimit_g; /* Maximum value */
char count_l; /* Count of input value */
short noOfPages_l = 0; /* Number of pages */
• In addition to the major suffix following suffix is to be used after the major suffixes.
o Pointer variables: 'p'
o Register: 'r'
o Static: 's'
e.g. node_t *nextNode_lp = NULL; /* Pointer to next node. */
• For software packages, which give a function call interface to the user, all the global
variables and functions used within the package must be protected from user programs.
• For example, you can decide to precede each such variable by E_ and function by F_.
And this should be specifically mentioned in the user manual.
Mapping:
Type Casting:
• Do not assume default conversions of C raw types. Explicit mention of casting is needed.
Operators:
Primary operators "->", "." and "[ ]" should be written with no space around them.
e.g.
houseRec_g->room = i_l;
studentInfo_l.grade . . .
subject_g[j_l] . . .
No space should be left between function name and its opening parenthesis and also between
opening parenthesis and first parameter; last parameter and closing parenthesis.
e.g.
convertVal(inval_i, outval_o);
There should not be any space between a unary operator and its operand.
e.g.
!finished_g
NoOfPoints_i
*house_lp
&inVal_g
sizeof(integer4)
++index
Commas should have one space after them.
e.g.
getParameters(filNam_g, noOfPrmts_l, prmtArr_g);
Other operators should have one space on either side of them.
e.g.
initval_g + displacement_l
number_l / MAX_NO_OF_VALUES
Functions:
Define a constant 'FUNCTION' as shown below.
#define FUNCTION
Write this constant before declaration of function.
e.g.
FUNCTION SINT4 convert(...)
FUNCTION void getNames(...)
Immediately after declaration of a function, give the following information about it as a comment.
• Purpose
• Input arguments
• Output arguments
• Return values
• Implicit input variables
• Implicit output variables
• Implicit i/o variables
• Calling functions (optional)
• Called functions
• Author
• Date written
• Log of changes (must contain date of making the change, Name of the person who is
making the change, and the reason for making the change)
• A comment for each local variable must be given.
• The type of value returned by a function must be specified along with declaration.
CMC Limited For Internal Use Only Page: 4 of 7
CMM Date: 15th Feb, 2003 Version No: 01
Internal Template Version: 04
Guidelines for C Effective Date: 28th Dec, 2001
General:
Indentation:
• Each line, which is a part of the body of a C control structure, should be indented one tab
stop (preferably two spaces) from the margin of its controlling line. The same rule applies
to structure/union definitions.
• Each opening and closing braces should be on a separate line and indented one tab stop
from the controlling keyword of C. Code inside the braces should be indented to the
same extent from them.
e.g.
if (a_l == 1)
x = y;
else
{
printf(" ... " , ...);
...
}
switch(c_g)
{
case a_g : ...
case b_g : ...
...
}
• A null statement appearing as the body of a loop must be placed in a separate line.
e.g.
nameLen = 0;
while (nameStr[nameLen++]) ; /* Null statement */
• Function definition should begin at column 1 of the line. All the parameter declarations,
and first level braces should also start at column 1. Body of the function should follow
indentation rules given above.
e.g.
FUNCTION SINT4 example(param1, param2, ...)
SINT4 param1;
UINT2 param2;
{
statement 1;
statement 2;
...
...
}
Modularity:
• Programs must be functionally divided into modules. All the operations on a data
structure must be preferably coded in the same physical file.
e.g.
The following functions operate on a data structure bitmap
set_bit()
clear_bit()
test_bit()
find_first_bit()
• All these functions can form a module and can be put in a file.
Include files:
• If the software for a project consists of large number of program files, the information
required by more than one program must be stored in a file and should be shared by all
the programs needing the information by including the file.
• There may be more than one such file. The point to be considered is that a program
should not have information visible to it, which is not used by the program.
Environmental Features:
• Segregate environment specific code into specific small functions. It helps in
minimizing and eases the work in porting software to a new environment.
e.g.
strcpy(drive_g, "/user/catalog"); /* Bad */
getDriveName(drive_g); /* Good - dependence is */
/* in small function. */
Writing Macros:
• Macros should be given upper-case names.
e.g.
MAX(x, y) /* Gives maximum of x and y */
ABS(x) /* Gives absolute value of x */
• Put parenthesis around each of the parameters in the replacement text and around the
entire replacement text.
e.g.
# define SQUARE(x) ((x) * (x)) /* Square of x */
Use of Comments:
• Each program file must include a brief description of its contents in the beginning. In
addition it must have a list of all the functions defined in this file along with the formal
parameters. The list must maintain the order in which functions physically appear in the
file.
• All the control structures involving evaluation of various conditions, branching or looping
must be very clearly documented to give a clear picture of the processing taking place.
For example all if-then-else, while, do-while, goto, switch etc. must be documented in
the program file along the code.
• Every logical group of statements should be commented before the start of the
statements. This comment should be preceded by at least one blank line.
• If a statement needs clarification, comment should be placed one tab space away from
the statement.
• Functions must be clearly demarcated from each other by a line consisting of
Closing of Files:
CMC Limited For Internal Use Only Page: 6 of 7
CMM Date: 15th Feb, 2003 Version No: 01
Internal Template Version: 04
Guidelines for C Effective Date: 28th Dec, 2001
• Explicitly close all the files opened by a program when the usage is over.
Diagnostic Messages:
• In order to help in subsequent debugging you must include printf statements for various
important variables at strategic places in all your programs. However these statements
must be enclosed within
#ifdef DEBUG
#endif
construct.
• These printf statements can be further divided into various levels depending on details
of information printed. The selection of a level can be controlled by use of a runtime
global variable for the entire program.
• The details could include diagnostic messages to trace the execution sequence of
various functions, printing of important parameters at entry to a function etc. You must
have the following for every function:
o A message indicating entry to the function
o A message indicating return from the function must be included in all return
paths.
Error messages:
• Error messages given by various programs should not be hardcoded.
All the messages should be stored in a file and must be accessed by using an error
number. For example an error message when printed can have the following format:
o S-M-F-N-Error message