C and C++ formatter — re-indent code that lost its structure

Paste C or C++ and get it re-indented from its braces, with #include and #ifdef lines left at the margin, access labels outdented and raw strings untouched.

Live demo · real result from this tool
Indent

Formatted C++

#include <iostream>#include <vector>class Stack {public:    void push(int v) { items.push_back(v); }    int pop() {        int v=items.back();        items.pop_back();        return v;    }private:    std::vector<int> items;};int main() {    Stack s; s.push(1'000);    switch (s.pop()) {        case 1000:            std::cout<<"thousand\n";            break;        default:            break;
    }
}

Indentation and spacing were normalised by brace structure — this is not a full C++ formatter, so line wrapping, blank-line rules and token spacing beyond the basics are left as written.

Processed on your device — nothing is uploaded.

How to use the C/C++ formatter

  1. Paste C or C++ source into the Input box. The sample has a Stack class and a switch.
  2. Set Indent (4 spaces by default, or 2 spaces or Tab) and Opening braces (Keep as written, Same line, Own line).
  3. Keep Normalise spacing ticked to get if (, ) { and } else, and choose Max blank lines in a row.
  4. Copy the Formatted C++ output. A note below it says how many lines inside literals were left alone.

What it does and when to use it

C and C++ code loses its indentation easily: copied from a PDF datasheet, pasted into a chat, merged from two editors with different tab settings, or produced by a code generator. This tool gives every block its correct depth again so you can follow the logic. It suits embedded C snippets, competitive-programming solutions and header files you need to read quickly.

Related tools: the C# formatter and Java formatter use the same engine with each language’s own rules, the code beautifier also covers Go, Rust and Swift, and the code diff viewer shows what changed.

How it works

The engine scans every line and hides the contents of strings, char literals, comments and raw strings R"delim( … )delim". On what is left it:

  • counts open {, ( and [ to find each line’s depth,
  • keeps preprocessor lines (#include, #define, #ifdef) at the left margin,
  • moves public:, private: and protected: back one level inside a class,
  • indents case labels inside switch, and their statements one level more,
  • applies the brace and spacing options to code only.

It never reorders tokens or changes operators, and it does not need the code to compile. Everything runs in your browser.

Worked examples

These are the tool’s own outputs.

The sample, default settings. The flat class gets its access labels at class level and its members indented:

class Stack {
public:
    void push(int v) { items.push_back(v); }

Further down, switch(s.pop()){ becomes switch (s.pop()) { with case 1000: one level in and std::cout<<"thousand\n"; one level deeper. s.push(1'000) causes no trouble.

Raw string plus K&R braces. With Opening braces set to Same line, the two lines inside R"( … )" stay exactly where they were, while } and else{ on separate lines join as } else {.

Preprocessor inside a function. #ifdef X and #endif stay at column 0 while return 1; is indented:

int f() {
#ifdef X
    return 1;
#endif
}

Limits and tips

  • Operators are not spaced. int v=items.back(); and std::cout<< stay as written.
  • Only ) { gets a space before the brace. namespace app{ and struct P{ keep theirs as typed.
  • Macros that hide braces can throw off the depth after them; check code that uses them.

Frequently asked questions

Is this clang-format?
No. clang-format parses C++ and applies a complete style, including line wrapping and operator spacing. This tool only rebuilds indentation from braces and brackets, optionally moves opening braces, and adds a few spaces. Use it to rescue unreadable code, and clang-format for project-wide style.
Why are my #include and #ifdef lines not indented?
Preprocessor directives are kept in column 0, which is the most common style and works with every compiler. Lines continued with a backslash, as in a multi-line #define, are kept as written.
Does a { inside a string or raw string confuse it?
No. Strings, char literals such as '{', comments and C++11 raw strings like R"( … )" are masked before braces are counted, so they never change the indent. Lines inside a multi-line raw string are copied exactly.
What about 1'000 and other digit separators?
The C++14 digit separator is recognised, so 1'000 is not mistaken for the start of a char literal and the lines after it indent correctly.
Which brace setting should I use?
The default, Keep as written, changes indentation only. Pick Same line (K&R) or Own line (Allman) to switch the whole file to one style; Allman puts the { of every function, class and if on its own line.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close