The coding style for C/C++ mostly follows the K&R Style.
This repository contains a Clang-format file which can be used to format the code automatically based on below rules.
- Indentation with 4 spaces (tabs not allowed)
- Space before pointer or reference symbols
*and&placed adjacent to name (e.g.int *var) - No space before opening brace of function:
void foo(int arg); - One space after
if,for,whileandswitchstatements. - Always use curly braces for
ifandforstatements, even for one line. - Curly braces for functions and classes start in next line, for flow statements (
if,while,switch,for) in the same line. - Access modifiers in C++ classes (
public,private,protected) are not indented. - Precompiler statemens (
#define,#ifdef, etc.) are not indented. - Maximum line length of 100 characters
- No spaces at end of a line or in an empty line
- One empty new-line at the end of the file
- Linux-style line-ending (LF or
\n)
- Class names use
PascalCaseorUpperCamelCase. Acronyms are treated as a single word (a JSON parser is namedJsonParser). - Struct names use
snake_case. A typedef with the same name inPascalCasemay be used, so that a struct object can be declared similar to a class in C++. Other typedefs for structs (e.g. short names with_tor_ssuffix) are not allowed. - Enums use
snake_casefor their name andUPPER_CASEfor their elements. Typedefs for enums are not allowed. - Function or method names use
snake_case. - Global and local variables, parameters and class/struct/union member names also use
snake_case. - Preprocessor macros and defines use
UPPER_CASE. - File names are always lower case to prevent incompatibilities between different operating systems.
- Each header file should contain header guard defines using the file path/name in upper case letters (e.g.
PATH_TO_MY_FILE_H_) without any underscores_at the beginning, as names with underscores are reserved for C libraries.
- C++/C99-style comments
//are allowed - Expressions behind defines must be enclosed in braces, e.g.
#define (3*1034) - Comments should be in Doxygen style where applicable.
- Order of includes (see also here): Related header, C system headers, C++ standard library headers, other project headers.
Header file example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
#include <stddef.h>
/**
* Character buffer to store strings incl. their maximum size
*/
struct buffer
{
char *str; ///< Pointer to string in memory
size_t size; ///< Maximum size of buffer
};
enum state
{
STATE_ON,
STATE_OFF,
};
/**
* Description of MyClass
*/
class MyClass
{
public:
MyClass();
private:
/**
* Description of member function.
*/
void member_func(char c);
/**
* This variable is used for foo.
*/
int member_var;
}
/**
* State machine of this nice embedded hardware
*
* @param buf Some description here
* @param foo Meaning of this variable here
*/
void state_machine(struct buffer *buf, int foo);
#endif /* EXAMPLE_H_ */
Implementation file example.cpp
#include "example.h"
static enum state current_state;
void state_machine(struct buffer *buf, int foo)
{
switch (current_state) {
case STATE_ON:
do_something();
break;
case STATE_OFF:
return;
break;
}
if (buf->str != NULL) {
// ...
}
else {
// ...
}
for (int i = 0; i < 10; i++) {
// ...
}
}