C++ Formatter and Beautifier

Paste cramped or messy C++ and get it back with consistent braces, spacing, and indentation. The formatter is clang-format, running in your browser, so your code stays on your device.

Indent
Style

Format uses clang-format. The style and indent options apply to Format.

Result

What a C++ formatter does

C++ gives you a lot of freedom over spacing and brace placement, and templates and long type names make it easy for code to get cramped. A formatter, also called a beautifier, rewrites the source with one consistent set of rules: braces in the same place, even indentation, and consistent spacing. This tool uses clang-format, a widely used C++ formatter, and changes only the layout, never what the program does.

Why use our C++ beautifier

  • ·Free. No signup, no paywall, no limits.
  • ·Runs in your browser. clang-format runs locally as WebAssembly, so your code is never uploaded.
  • ·Real styles. Format to Google, LLVM, Mozilla, and other well-known C++ styles.
  • ·Your house style. Choose 2 or 4 space indentation to match your project.
  • ·Syntax highlighting. The editor colours keywords and types, and lets you collapse and expand blocks.

C++ formatter options

Format

Rewrites your C++ with consistent braces, spacing, and indentation using clang-format. Turns cramped or messy code into clean, readable source.

Beforeclass A{int x;};Afterclass A { int x; };

Style

Pick the base style clang-format follows: Google, LLVM, Mozilla, WebKit, and more. Each sets brace placement, spacing, and other rules to match a well-known C++ style.

BeforeGoogle, LLVM, Mozilla...AfterYour chosen house style

Indent

Choose 2 or 4 spaces for each level of nesting, so the formatted C++ matches the style used in the rest of your project.

Before2 spaces or 4 spacesAfterYour choice per level

When to format C++

A formatter helps whenever C++ source is hard to read or needs to match a shared style. These are the cases that come up most:

  • ·Matching a project style. Reformat code to Google or LLVM before you commit it.
  • ·Cleaning up pasted code. Clean up a snippet from a forum, a textbook, or a code generator.
  • ·Reviewing a pull request. Make dense template code readable before you review or merge it.
  • ·Learning C++. See how well-structured C++ is laid out while you are learning.

Learn more about C++

Once your C++ is formatted, these references explain the language and the formatter behind this tool:

Run clang-format on your own C++ code

This tool runs clang-format, the standard formatter for C++, compiled to run in your browser. The same styles work on your machine:

ToUse
Format a file in placeclang-format -i main.cpp
Pick a base styleclang-format -i --style=Google main.cpp
Save a style for the projectclang-format --style=LLVM --dump-config > .clang-format
Format on saveThe C/C++ extension in VS Code and CLion both use a .clang-format file if one is present

When a .clang-format file is in your project folder, clang-format uses it automatically, so everyone on the team gets the same layout.

If you work across languages, these formatters run the same way, right in your browser:

Frequently asked questions

What is a C++ formatter?
A C++ formatter takes source code that has cramped or messy spacing and rewrites it with consistent braces, spacing, and indentation so it is easy to read. This tool uses clang-format, the same formatter built into many C++ toolchains, and does not change what your program does, only how the code is laid out.
How do I format C++ code?
Paste your C++ into the input box, pick a base style and indentation, then click Format. The clean version appears below. Click the copy button to copy it, or download it as a .cpp file.
Does formatting change how my C++ program works?
No. Formatting only changes whitespace, braces, and line breaks. The classes, templates, functions, and logic stay the same, so the program compiles and runs exactly as before.
What are the style options?
The style options are the base styles clang-format supports, such as Google, LLVM, Mozilla, WebKit, Chromium, Microsoft, and GNU. Each one sets a full set of rules for brace placement, spacing, and alignment. Pick the one that matches your project.
Is this a C++ beautifier?
Yes. Format and beautify mean the same thing here: taking messy C++ and laying it out cleanly with proper indentation and spacing. Some people call it a C++ beautifier, prettifier, or pretty printer.
Is my C++ code kept private?
Yes. The formatter is clang-format compiled to WebAssembly and runs entirely in your browser. Your code is never uploaded to a server or saved anywhere, so you can safely paste in code from a private project. Once the page has loaded you can even use it offline.