C++ Formatter

Format C++ with clang-format.

Runs locallyYour input is processed in this tab. The editor blocks outgoing network requests.
C++ input

The C++ Formatter runs clang-format, compiled to WebAssembly, on C++ source and headers. Templates, classes with access specifiers, namespaces, lambdas and range-based loops are laid out in your choice of seven predefined styles, from LLVM and Google to Microsoft and GNU. It is for C++ developers tidying code before review. Everything runs in your browser — nothing is uploaded.

How to format C++ code

  1. Paste your code, open a .cpp, .cc, .cxx, .hpp or .hh file with ⌘/Ctrl O, or load an example.
  2. Choose a Style that matches your codebase; LLVM is the default, and switching styles reformats immediately.
  3. Copy the result with ⌥/Alt C, download it with ⌘/Ctrl S, or choose Switch to C for plain C files.

Examples

Template function and main in LLVM style

LLVM keeps a short template declaration on the same line as the function and puts an unbraced loop body on its own line.

C++
#include <iostream>
#include <vector>
template<typename T> T sum(const std::vector<T>& v){T s{};for(auto& x:v)s+=x;return s;}
int main(){std::vector<int> v{1,2,3};std::cout<<sum(v)<<std::endl;return 0;}

Result:

C++
#include <iostream>
#include <vector>
template <typename T> T sum(const std::vector<T> &v) {
  T s{};
  for (auto &x : v)
    s += x;
  return s;
}
int main() {
  std::vector<int> v{1, 2, 3};
  std::cout << sum(v) << std::endl;
  return 0;
}

A class in Google style

Google style indents public: and private: by one space, keeps the one-line push on a single line, and separates the two sections with a blank line.

C++
class Stack{public:void push(int v){d.push_back(v);}int pop(){int v=d.back();d.pop_back();return v;}private:std::vector<int> d;};

Result:

C++
class Stack {
 public:
  void push(int v) { d.push_back(v); }
  int pop() {
    int v = d.back();
    d.pop_back();
    return v;
  }

 private:
  std::vector<int> d;
};

Namespace and lambda in Chromium style

Chromium breaks a lambda body that would exceed 80 columns, keeps nested template brackets as >>>, and closes the namespace with a comment.

C++
namespace app{
auto by_total=[](const Order& a,const Order& b){return a.total>b.total;};
std::map<std::string,std::vector<std::pair<int,double>>> index;
}

Result:

C++
namespace app {
auto by_total = [](const Order& a, const Order& b) {
  return a.total > b.total;
};
std::map<std::string, std::vector<std::pair<int, double>>> index;
}  // namespace app

Options explained

  • Style — picks one of clang-format's predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft or GNU); each fixes indent width, brace placement, pointer and reference alignment, line limit and the C++-specific rules compared below.

Common C++ formatting problems

clang-format does not compile the code, so it rarely fails outright. When the result looks wrong, the cause is usually one of these.

Everything after one function is indented too far

A brace is missing. clang-format formats unbalanced input without reporting an error, and the extra depth carries on to the end of the file. Look for the first place the indentation stops matching the structure.

A namespace comment appeared that I did not write

LLVM, Google, Chromium and Microsoft add // namespace name after the closing brace of a namespace. It is a comment only and does not change the code. Choose Mozilla, WebKit or GNU if your project does not use these comments.

public: moved to a different column

Each style places access specifiers differently (see the table above). The position is fixed by the style and cannot be adjusted separately.

Include order changed

All styles sort #include lines within each block separated by blank lines, and Google and Chromium regroup system headers before project headers. Add a blank line to keep a header that must come first in its own block.

Some code must stay hand-formatted

Wrap it in // clang-format off and // clang-format on. This is useful for aligned tables, constexpr lookup arrays and test data.

FAQ

Is this the real clang-format, and which options can I change?

It is clang-format from the LLVM project compiled to WebAssembly, so each predefined style formats exactly as clang-format --style=<name> does. The only setting is the style itself; a .clang-format file from your repository is not read, and individual options such as ColumnLimit or IndentWidth cannot be overridden on their own.

Does it understand modern C++ such as concepts, lambdas and structured bindings?

Yes. clang-format tokenises C++ with the same lexer as the Clang compiler, so C++20 and C++23 syntax — concepts and requires clauses, structured bindings, designated initialisers, lambdas with captures, auto return types and nested templates closing with >> — are formatted without special configuration. Because it is not compiling, it also formats code that uses types it has never seen declared.

Is my code uploaded to a server?

No. clang-format runs as WebAssembly inside your browser, and the editor is an isolated frame that blocks network requests, so nothing is uploaded or stored on a server. Your chosen style is remembered on this device, and the code itself is only kept if you turn on "Remember my last input".

Should I use the C++ Formatter or the C Formatter for a header file?

Either gives the same result, because both run the same clang-format engine with the same styles. The C Formatter opens .c and .h files; this tool opens .cpp, .cc, .cxx, .hpp and .hh. Choose whichever matches the file you have, and use the Next action to switch without re-pasting.

Which style should I use for a new project?

Pick the style your dependencies or team already use, since consistency matters more than any single rule. Google style is common in open-source libraries and at companies that follow the Google C++ Style Guide; LLVM suits projects built around Clang tooling; Microsoft suits Windows codebases that put every brace on its own line and allow 120 columns.

Why did a short function stay on one line?

Most styles keep a short member function defined inside a class on one line, as in void push(int v) { d.push_back(v); }, when the whole definition fits within the line limit. Microsoft and GNU expand it with the braces on their own lines. The rule is part of the style and changes with it.

For code in other languages, see the Java Formatter, the Go Formatter and the rest of the code tools.

C++ style differences at a glance

The styles differ most visibly in how they treat C++ constructs that C does not have.

Style Template declaration Access specifiers Namespace closing comment References
LLVM Same line if short Flush left } // namespace app T &v
Google Own line Indented 1 space } // namespace app T& v
Chromium Own line Indented 1 space } // namespace app T& v
Mozilla Own line, template< Flush left None T& v
WebKit Own line Flush left None T& v
Microsoft Same line if short Indented 2 spaces } // namespace app T &v
GNU Own line Flush left None T &v

Browse all Code Formatters