FormatterHQ

C++ Code Formatter

Format modern C++ with clang-format: templates, namespaces, lambdas and access specifiers, in Google, LLVM, Chromium, Mozilla, WebKit, Microsoft or GNU style.

Input CPP
Output CPP
Ready.

How to use the C++ Formatter

  1. Paste your C++ source code into the Input editor, or use Sample, Upload file or Load URL.
  2. Pick a Style preset (default: Google), keep Indent on Style default or override it, and set the Column limit (0 means no limit).
  3. Press Format; with Auto update on, the output refreshes as you edit.
  4. Use Copy or Download above the output panel to take the formatted file with you.

A C++ formatter using clang-format

This C++ formatter runs clang-format, the de facto standard formatter for C++, compiled to WebAssembly so it runs in your browser. It understands templates, namespaces, lambdas, access specifiers, attributes and modern C++ syntax up to recent standards. Choose a style preset and get output that matches what clang-format would produce on the command line with the same options.

Example

#include <map>
#include <string>
#include <algorithm>
namespace app{template<typename T>class Registry{public:void add(const std::string&key,T value){items_[key]=std::move(value);}std::optional<T> get(const std::string&key)const{auto it=items_.find(key);if(it==items_.end())return std::nullopt;return it->second;}private:std::map<std::string,T> items_;};}

With the Google preset:

#include <algorithm>
#include <map>
#include <string>
namespace app {
template <typename T>
class Registry {
 public:
  void add(const std::string& key, T value) { items_[key] = std::move(value); }
  std::optional<T> get(const std::string& key) const {
    auto it = items_.find(key);
    if (it == items_.end()) return std::nullopt;
    return it->second;
  }

 private:
  std::map<std::string, T> items_;
};
}  // namespace app

Several Google C++ Style rules are visible: the includes are sorted, public: and private: are indented by one space, & binds to the type (const std::string& key), a short method body stays on one line, and clang-format added a // namespace app comment to the closing brace. The code inside the namespace is not indented.

Presets at a glance

PresetIndentPointer/referenceNotes
Google2int* pAccess modifiers at +1, short ifs on one line
Chromium2int* pGoogle-based; one parameter per line when they do not fit
LLVM2int *pAccess modifiers flush left
Mozilla2int* pReturn type on its own line for definitions
WebKit4int* pFunction braces on a new line
Microsoft4int* pAllman braces
GNU2int *pGNU brace indentation, space before parentheses

Mozilla style comes from the Firefox code base and WebKit style from the WebKit browser engine, so they are the natural choice when contributing to those projects. The difference between Google and Chromium shows up with long parameter lists: Google fills lines as far as the column limit allows, while Chromium puts each parameter on its own line once they do not all fit.

Options

  • Style – the BasedOnStyle preset.
  • Indent – keep the style's width or override it; tabs are 4 columns wide.
  • Column limit – 100 by default. Google and LLVM styles officially use 80; set 80 to match them exactly, or 0 to disable wrapping.

Modern C++ support

clang-format handles auto and structured bindings (auto [key, value] = *it;), range-based for, lambdas with captures, constexpr and consteval, concepts and requires clauses, attributes such as [[nodiscard]], and raw string literals, which are left untouched. Template angle brackets are closed as >> where C++11 allows it.

Limits and tips

  • It is not a compiler. Syntax errors do not produce messages; they produce strange indentation. Check the first line where the layout looks wrong.
  • Macros that expand to statements or declarations can confuse the layout. clang-format has options for this in a config file, which this tool does not expose.
  • Protect a region with // clang-format off and // clang-format on comments, for example around a hand-aligned lookup table. The markers work here just as they do on the command line.

For plain C use the C Formatter (LLVM preset by default). To generate C++ classes from JSON, see JSON to C++. Your code stays in your browser.

Frequently asked questions

Is this the real clang-format?
Yes. The tool runs clang-format compiled to WebAssembly, so the formatting rules are the same as the command-line tool with the chosen preset.
How do I keep a block of code unformatted?
Surround it with // clang-format off and // clang-format on comments. clang-format leaves everything between the markers exactly as written.
Why was a comment added after my namespace?
Most presets enable FixNamespaceComments, which adds // namespace name after the closing brace of longer namespaces so you can see which block ends there.
Why did the column limit differ from Google style’s 80?
The tool uses 100 by default because many projects prefer it. Set Column limit to 80 to match Google and LLVM style exactly.
Does it support C++20 features?
clang-format understands concepts, requires clauses, designated initializers and other C++20 syntax, and formats them consistently.

Last updated September 26, 2026

Related tools