C Language

C CODING SUGGESTIONS

  1. Don't use = in an if statement expression, unless it is absolutely necessary.
  2. Define a macro EQ for ==, and never use ==.
  3. Define macros for &, |, &&, and ||.
  4. Define macros for BOOL, TRUE, and FALSE.
  5. Use only Boolean-valued expressions in if statements.
  6. Use only Boolean variables with the logical operators && and ||.
  7. Do all assignments as separate statements, not as part of a more complex expression.
  8. Use parentheses in expressions to explicitly define order of evaluation.
  9. Don't use %i format specifications or numbers that begin with 0.
  10. Be sure to code addresses for arguments to scanf and similar functions.

  1. Always enclose conditional code in braces.
  2. Do all assignments as separate statements, not as part of a more complex expression.
  3. Use parentheses around expressions on return statements.
  4. Never use the C switch statement.
  5. Place the opening /* and closing */ for comments on lines by themselves, and use a | to begin each line of comment text.

  1. Declare C arrays with one extra element, and don't use the element with subscript 0.
  2. Use macros to define tables and loops over them.
  3. Always guard a string assignment against overwriting the target variable.
  4. Create macros and functions to define strings and provide "safe" string operations.

  1. Declare functions static if you intend to call them only from within the same source file.
  2. Use the most restricted visibility possible for variables; avoid shared variables.
  3. Put all external declarations before the first function definition in a file.
  4. Put functions that must share data and external declarations for their shared variables in a file by themselves.
  5. Use EXPORT, SHARE, and IMPORT macros to clarifythe intended visibility of a variable.

  1. For non-array "output" or "input/output" parameters, use local variables instead of dereferenced parameters in function calculations.
  2. Use array notation instead of pointers anddereferencing when you're working with arrays.
  3. When working with pointers in assignment statements, double-check that you're using the right level of indirection.
  4. Unless you're positive a pointer has been initialized, check it for NULL before using it.
  5. Use a new_string function to return new strings from functions.
  6. Always check for a NULL return value after calling malloc.
  7. Use the allocate macro to prevent memory "leakage."
  8. Always initialize pointers in their definitions.
  9. Use typedef and PTR, contents_of, and address_of macros to improve program readability.
  10. Avoid popular, but tricky, C idioms for business application programming.
  11. Don't use ++ or - in assignments.
by BrainBellupdated
Advertisement: