C++ Beautifier
Format and beautify C++ source code online with configurable indent styles, brace placement, and spacing. Paste, upload, or type your code.
About
Misformatted C++ code increases cognitive load and introduces merge conflicts in version control. A single inconsistent indentation pattern across a codebase with 50+ source files can cost hours of review time per sprint. This tool tokenizes raw C++ input and reconstructs it according to configurable style rules: indent width (2, 4, or 8 spaces, or tabs), brace placement (K&R, Allman, Stroustrup), and operator spacing. It handles preprocessor directives, template parameters, nested namespaces, access specifiers, single-line and multi-line comments, and raw string literals.
The formatter approximates the behavior of tools like clang-format for common style profiles. It does not perform semantic analysis or macro expansion. Code with deeply nested preprocessor conditionals or non-standard compiler extensions may not format perfectly. All processing runs in your browser. No code is transmitted to any server.
Formulas
The beautifier operates as a two-stage pipeline: tokenization followed by reconstruction.
Each token Ti carries a type (keyword, identifier, operator, literal, comment, preprocessor, brace, semicolon) and its raw text value. The formatter then walks the token stream and emits output lines:
Indentation depth d is tracked as a counter. On encountering {, d increments: d ← d + 1. On }, it decrements: d ← max(0, d − 1). Each output line is prefixed with d × w spaces, where w is the configured indent width. Access specifiers (public:, private:) are emitted at depth d − 1 to visually separate them from member declarations.
Reference Data
| Style Option | K&R | Allman | Stroustrup | Description |
|---|---|---|---|---|
| Opening brace (function) | Same line | New line | New line | Placement of { after function signature |
| Opening brace (if/for/while) | Same line | New line | Same line | Placement of { after control statements |
| Opening brace (class/struct) | Same line | New line | Same line | Placement of { after class declaration |
else placement | Same line as } | New line | New line | Where else appears relative to closing brace |
| Indent width (spaces) | 4 | 4 | 4 | Configurable: 2, 4, 8, or tab |
| Namespace body indent | No | No | No | Whether to indent contents inside namespace {} |
| Access specifier indent | −1 level | −1 level | −1 level | public:, private:, protected: dedented one level |
| Spaces around operators | Yes | Yes | Yes | E.g., a = b + c vs a=b+c |
| Spaces inside parentheses | No | No | No | E.g., if (x) vs if( x ) |
| Blank line limit | 2 | 2 | 2 | Max consecutive blank lines allowed |
| Trim trailing whitespace | Yes | Yes | Yes | Remove spaces/tabs at end of lines |
| Space after keywords | Yes | Yes | Yes | if ( vs if( |
| Pointer alignment | Right (int *p) | Right | Right | Star/ampersand alignment in declarations |
| Max line length (soft) | 120 | 120 | 120 | Advisory; no automatic line breaking |