import { _ as _export_sfc, c as createElementBlock, o as openBlock, a8 as createStaticVNode } from "./chunks/framework.Clpp4x2N.js"; const __pageData = JSON.parse('{"title":"Coding Conventions (C)","description":"","frontmatter":{},"headers":[],"relativePath":"coding_conventions_c.md","filePath":"coding_conventions_c.md"}'); const _sfc_main = { name: "coding_conventions_c.md" }; const _hoisted_1 = /* @__PURE__ */ createStaticVNode('
Most of our style is pretty easy to pick up on, but right now it's not entirely consistent. You should match the style of the code surrounding your change, but if that code is inconsistent or unclear use the following guidelines:
if (condition) { return false; }
if (condition) return false;
/* */
#pragma once
at the start of header files rather than old-style include guards (#ifndef THIS_FILE_H
, #define THIS_FILE_H
, ..., #endif
)#ifdef DEFINED
and #if defined(DEFINED)
#if defined(DEFINED)
form.#if
.#
and if
, starting with 4 spaces after the #
.Here is an example for easy reference:
/* Enums for foo */\nenum foo_state {\n FOO_BAR,\n FOO_BAZ,\n};\n\n/* Returns a value */\nint foo(void) {\n if (some_condition) {\n return FOO_BAR;\n } else {\n return -1;\n }\n}
Clang-format is part of LLVM and can automatically format your code for you, because ain't nobody got time to do it manually. We supply a configuration file for it that applies most of the coding conventions listed above. It will only change whitespace and newlines, so you will still have to remember to include optional braces yourself.
Use the full LLVM installer to get clang-format on Windows, or use sudo apt install clang-format
on Ubuntu.
If you run it from the command-line, pass -style=file
as an option and it will automatically find the .clang-format configuration file in the QMK root directory.
If you use VSCode, the standard C/C++ plugin supports clang-format, alternatively there is a separate extension for it.
Some things (like LAYOUT macros) are destroyed by clang-format, so either don't run it on those files, or wrap the sensitive code in // clang-format off
and // clang-format on
.