C++ Code Formatter
Re-indent C and C++ source code by tracking brace ({ }) nesting depth — every block gets consistent indentation, with comments and string literals left untouched.
Input
Formatting Options
Output
Guides
What this tool does
C and C++ source picked up from a forum post, a diff, an AI-generated snippet, or a file that's been through one too many hand edits tends to end up with inconsistent indentation — tabs mixed with spaces, if bodies at the wrong depth, closing braces that don't line up with the line that opened them. This tool re-indents that code by tracking brace ({ / }) nesting depth line by line: every line is reset to depth × indent-size spaces, and a closing } is dedented before it's printed so it lines up cleanly with its opening brace.
It is a brace-depth re-indenter, not a full C++ formatter. It does not parse C++ grammar, does not rewrite operator spacing, does not enforce a brace style (Allman vs. attached), does not align pointers (int* p vs int *p), and does not wrap long lines to a column limit — the kind of things a real tool like clang-format does. What it fixes is the single most common readability problem: lines sitting at the wrong indentation level. Everything else about a line — the tokens, the spacing between them, comment wording — is left exactly as you wrote it; only the leading whitespace changes.
How to use it
- Paste your C or C++ code into the C / C++ Code box, or upload a source file directly.
- Choose an Indent Size — 2 or 4 spaces per nesting level (4 is the default and matches common C++ style guides).
- The re-indented result appears instantly in the Formatted Code panel. Copy it or download it as
formatted.cpp.
Formatting happens automatically as you type or edit the input — there's no button to click.
How the indentation logic works
The formatter reads your code line by line and keeps a running nesting depth. A line is scanned for net {/} count: each { increases the depth that applies to lines after it, each } decreases the depth. A line whose first character is } is dedented one level before it's printed, so } (and } else {) line up visually with the line that opened the block.
Because indentation should not be thrown off by a stray brace that isn't really code, the scanner skips brace characters that appear inside "..." string literals, '...' character literals, // line comments, and /* ... */ block comments — including block comments that span multiple lines, during which depth tracking is frozen until the closing */ is found. This covers the overwhelming majority of real-world C/C++ source. A handful of edge cases are intentionally out of scope for this lightweight approach: a genuine C++ lexer would also need to handle raw string literals (R"(...)"), multiple closing braces at the start of one line (each gets treated as a single dedent step), and preprocessor macros that redefine brace-like tokens.
If braces in your input are unbalanced — a missing }, or an extra one — the formatter never errors or gets stuck. Depth simply stops decreasing once it reaches zero, so the rest of the file still formats sensibly instead of collapsing to negative indentation.
FAQ
Does this behave like clang-format? No. clang-format fully parses C++ and can rewrite brace style, operator spacing, pointer alignment, and line wrapping to match a named style (LLVM, Google, WebKit, etc.). This tool only fixes indentation depth based on brace nesting — a much smaller, but still very useful, slice of what makes code readable.
Will it change my code logic or rewrite tokens? No. Only the leading whitespace of each line is replaced. Everything else — spacing inside a line, string contents, comment text, variable names — is left exactly as written.
Does it work with plain C, not just C++? Yes. The formatter only looks at brace nesting and comment/string syntax, both of which are identical in C and C++.
What happens with unbalanced braces?
The depth counter is clamped at zero — it never goes negative — so a missing or extra } degrades gracefully instead of producing garbled output for the rest of the file.
Can it format multiple files at once? Use the tool's batch mode (where available) to run the same indentation settings across a queue of uploaded files.
Privacy
This tool runs entirely in your browser. Your source code — including any comments, strings, or file paths it contains — is never uploaded to a server.