Skip to content

C Standards

Sydney Erickson edited this page Feb 19, 2018 · 13 revisions

Naming

  • Function names should use underscores between the name, in all lower case. Example: my_function
  • Variable names be camelCase. Example: myVar
  • Global variables (like #DEFINE) should use all caps underscores. Example: GLOBAL_VARIABLE
  • Function and variable names should describe it's use. Generic names should be avoided, except for for loops, where variables like x, y, and i are allowed as long as it's apparent what it's being used for.

Spacing

  • Spaces should exist between function call parameters.
  • 4 spaces should be used.
  • Function opening braces are on the same line as the definition.
  • A single blank line should separate blocks of code based on what they do.

Commenting

  • You should comment each block with what it does.
  • if and for statements should be commented at the beginning to explain what the conditions are.
  • Keep comments updated.
  • Comment all functions, including what each argument does. Check and see if DocBlockr is supported on your text editor, it helps a lot.

Example

/**
 * Runs a preset math operation on two integer inputs.
 * @param  myVar1 Base integer
 * @param  myVar2 Data integer
 * @return        The resulting integer after our operations
 */
int my_function(int myVar1, int myVar2) {
    // Run some math operations on our input variables
    return some_math_function(myVar1 + myVar2);
}
Clone this wiki locally