User Rating 0.0
Total Usage 0 times
Input 0 chars
Output
Is this tool helpful?

Your feedback helps us improve.

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.

c++ beautifier c++ formatter code beautifier cpp format code formatting c++ pretty print source code formatter

Formulas

The beautifier operates as a two-stage pipeline: tokenization followed by reconstruction.

tokenize(source) [T0, T1, …, Tn]

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:

format([T0Tn], config) output

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 OptionK&RAllmanStroustrupDescription
Opening brace (function)Same lineNew lineNew linePlacement of { after function signature
Opening brace (if/for/while)Same lineNew lineSame linePlacement of { after control statements
Opening brace (class/struct)Same lineNew lineSame linePlacement of { after class declaration
else placementSame line as }New lineNew lineWhere else appears relative to closing brace
Indent width (spaces)444Configurable: 2, 4, 8, or tab
Namespace body indentNoNoNoWhether to indent contents inside namespace {}
Access specifier indent−1 level−1 level−1 levelpublic:, private:, protected: dedented one level
Spaces around operatorsYesYesYesE.g., a = b + c vs a=b+c
Spaces inside parenthesesNoNoNoE.g., if (x) vs if( x )
Blank line limit222Max consecutive blank lines allowed
Trim trailing whitespaceYesYesYesRemove spaces/tabs at end of lines
Space after keywordsYesYesYesif ( vs if(
Pointer alignmentRight (int *p)RightRightStar/ampersand alignment in declarations
Max line length (soft)120120120Advisory; no automatic line breaking

Frequently Asked Questions

Preprocessor directives are always emitted at column zero, regardless of the current brace indentation depth. This matches the behavior of most C++ style guides (Google, LLVM, Mozilla). The formatter preserves the original preprocessor line intact and does not attempt to indent or reformat its contents.
The tokenizer distinguishes template angle brackets from comparison operators by tracking context: if the preceding token is a type name, keyword like "template", or closing angle bracket, the "<" is treated as a template opener. Nested templates like std::map> are handled correctly. However, extremely complex SFINAE expressions with dependent types may occasionally be misclassified.
The tokenizer recognizes C++11 raw string literals. The content inside the delimiter is preserved verbatim with no whitespace modification. The opening and closing markers are kept on their original lines.
Yes. If a control keyword (if, for, while, else) is followed by a statement that is not an opening brace, the formatter places the statement on the next line with one additional indent level. It does not automatically insert braces around single-statement bodies.
Plain C code will format correctly since C is a syntactic subset of C++. Objective-C uses @ directives and message-passing syntax ([obj method]) that the tokenizer does not specifically handle. Results for Objective-C will be approximate.
The tool processes files up to 5 MB in the browser. Files under 100 KB format nearly instantly. Larger files may take 1-3 seconds. The UI displays a progress indicator for any operation exceeding 200 ms.