This file and repo dictates the coding style to use when contributing C code on Club RockÉTS repositories.
Code should be idented with 4 spaces.
Code should not contain trailing whitespace but source files should end with a newline.
Braces should never be dropped, and should be placed on the same line as the condition, except for function names (K&R style).
// Good
int main()
{
if (condition) {
something();
}
do {
// Stuff
} while (true);
}
// Bad
void java() {
if (condition)
something();
do
{
// Stuff
}
while (true);
}
Pointer types should be written with a space between the type name and the *
(this means there is no space before the identifier).
// Good
int *a;
float *b, *c;
// Bad
int* a;
float* b,* c;
Macros composed of more than one line should be wrapped in a do { ... } while (0)
block.
#define DO_SOMETHING(param1, param2) \
do {\
function_alpha(param1);\
function_bravo(param2);\
} while (0)
All header files should contain an header guard. The header guard define should be the file name in ALL_CAPS, with the .
substituded for an _
, and a trailing _
added.
#ifndef KARMAN_FILTER_H_
#define KARMAN_FILTER_H_
// karman_filter.h
#endif
File should be named in snake_case.
Functions should be in snake_case. They should be prefixed with the module name.
// Good
int mti_set_config();
void karman_filter_data(int len, int *data);
// Bad
void PowerGet();
void doSomethingElse(int parameter);
Macros should be in ALL_CAPS.
#define PI 3.141592
#define DO_SOMETHING(param) do { something(param); } while (0)
Constant variables should be in ALL_CAPS. File and function scope variables should be named in snake_case.
const int SPEED_OF_LIGHT = 299792468;
int function()
{
int param;
float pi = 3.141592f;
}