Skip to content

Autoformat c code

To enforce basic rules it is practical to do this automatically. Clang format could be configured through a text file in the repository root. After that it is possible to run the formatting through the command line, an IDE like VSCode, a git pre commit hook or in a CI/CD build chain.

A sample configuration file could be generated with clang-format -style=llvm -dump-config > .clang-format. The style options are described in the llvm documentation.

Clang format file

---
BasedOnStyle: LLVM
IndentWidth: 4
AlignConsecutiveMacros: true
AlignConsecutiveDeclarations: true
BreakBeforeBraces: Custom
BraceWrapping:
  AfterFunction: true
PointerAlignment: Left
...