Developer and Text Tools

Choosing a clang-format Style for C and C++: LLVM, Google, Mozilla and More

What the main clang-format styles do with braces, pointers, includes and access modifiers, and how to build and test a .clang-format file in the browser.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
Choosing a clang-format Style for C and C++: LLVM, Google, Mozilla and More

A .clang-format file is often the first file added to a new C or C++ repository, and also the one that gets copied from somewhere else without anyone reading it. It usually starts with one line, BasedOnStyle, followed by a handful of overrides. Knowing what each base style actually does makes it much easier to pick one and to explain the choice to the rest of the team. The C and C++ Code Formatter lets you try each style on your own files and see the differences as a diff.

The six base styles at a glance

LLVM is the default. It indents by two spaces, keeps opening braces on the same line, writes pointers as int *p with the star next to the name, leaves case labels level with their switch, and puts public: and private: back at the column of the class.

Google is close to LLVM but attaches the star to the type, as in int* p, indents case labels, and places access modifiers one space in from the class. Chromium follows Google with a few extra rules that matter mostly for line wrapping.

Mozilla puts the opening brace of functions, classes and enums on its own line while keeping control statements attached. WebKit does the same for functions only and uses four-space indentation. Microsoft breaks before every brace and uses four spaces, which will feel familiar to anyone coming from C#.

Pointers and references

PointerAlignment decides where the star and ampersand go in a declaration. Right gives char name, Left gives char name, and Keep leaves each declaration as written. The choice is cosmetic, but mixing both in one file makes declarations harder to scan. The formatter only treats a star as a pointer when it sits between a type and a name, so multiplication in an expression like a * b keeps its spaces.

How includes are ordered

SortIncludes sorts #include lines, and the base style decides how. Each include gets a category. The header with the same name as the source file, such as shape.h for shape.cpp, gets the top priority and goes first. LLVM then ranks quoted project headers above system headers in angle brackets. Google separates C system headers like stdio.h, then C++ standard headers like vector, then project headers.

IncludeBlocks controls blank lines. Preserve, used by LLVM, keeps your own blank-line blocks and sorts inside each one. Regroup, used by Google, merges all includes and inserts a blank line between categories. If your project relies on one header being included before another, put a blank line between them and use Preserve.

Building your .clang-format

Start by choosing the base style closest to your existing code, then open a few real files and read the diff. Change only the keys where the base style disagrees with the code you have. Common overrides are IndentWidth, ColumnLimit, BreakBeforeBraces and PointerAlignment.

When the diff looks right, open the .clang-format section, press Show current and download the file. If you already have a .clang-format, paste it there and press Apply to options; keys the built-in engine reads update the form, and the rest are listed so you know which ones only the official build understands.

Built-in engine or official clang-format

The default engine is a compact TypeScript formatter that works offline. It handles preprocessor lines with backslash continuations, raw string literals, char literals and digit separators, and it never changes strings, comments or macros. It does not reflow code, so a line over ColumnLimit is reported rather than wrapped.

For exact clang-format output, press the button to load the WebAssembly build of clang-format 23.1.1. The page shows its size, about 2.6 MB, before downloading it from jsDelivr, and then formats your file with the same style keys. Everything still runs locally.

Checks worth reading

The issues tab counts #if, #ifdef and #ifndef against #endif, which catches a missing #endif long before the compiler reports a confusing error at the end of a header. Headers without an include guard or #pragma once are flagged, and so is using namespace in a header, since it leaks into every file that includes it. Mismatched brackets are reported with the line and column of the bracket that was opened.

Rolling a style out

Format the whole tree in one commit that changes nothing else, list that commit in a .git-blame-ignore-revs file, and add the .clang-format to the repository in the same change. After that, reviewers can ask for formatting fixes by pointing at the tool rather than at a person's habits.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages