embedded software boot camp

Coding Standard Rule #10: Don’t Use the Comma Delimiter Within Variable Declarations

Monday, April 6th, 2009 by Michael Barr

Rule: The comma (‘,’) operator shall not be used within variable declarations.

Example (don’t):

char * x, y;   // did you want y to be a pointer or not?

Reasoning: The cost of placing each declaration on a line of its own is low. By contrast, the risk that you’ve made a mistake and the compiler or a maintainer won’t understand your intentions is high.

Coding Standard Rule #9

These rules are excerpts from the Embedded C Coding Standard book.

Tags: , , ,

3 Responses to “Coding Standard Rule #10: Don’t Use the Comma Delimiter Within Variable Declarations”

  1. Steve M. says:

    As part of this rule, it may also be worth adding that in pointer declarations, the ‘*’ should appear before the variable name without a space.E.g.: char *x;

  2. Anonymous says:

    I would also like to add: Be consistent with the use of qualifiers on pointers.const char *p1;char const *p2;Some programmers prefer p1 because it keeps the pointer and type together (char and *). Some prefer p2 because it associates the const with the *.You could argue either way, but I've seen code using both declarations and I find it just a bit harder to read.Bill A.

  3. Dean T.H. says:

    I've always appreciated Dan Sak's instruction at a previous Embedded Systems Conference regarding "CV" variable definitions (i.e. variables that have the qualifiers "const" or "volatile").What's the difference between the following:char * p1;char const * p2;char * const p3;char const * const p4; Answer:p1 is a pointer to a charp2 is a pointer to a constant charp3 is a constant pointer to a charp4 is a constant pointer to a constant charThe trick: you read the variable definition backwards. Start with the variable name and work your way from right to left.Given that understanding, char const *p1;is preferred toconst char *p1;

Leave a Reply