How to use the C/C++ formatter
- Paste C or C++ source into the Input box. The sample has a
Stackclass and aswitch. - Set Indent (4 spaces by default, or 2 spaces or Tab) and Opening braces (Keep as written, Same line, Own line).
- Keep Normalise spacing ticked to get
if (,) {and} else, and choose Max blank lines in a row. - 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:andprotected:back one level inside a class, - indents
caselabels insideswitch, 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();andstd::cout<<stay as written. - Only
) {gets a space before the brace.namespace app{andstruct 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?
Why are my #include and #ifdef lines not indented?
Does a { inside a string or raw string confuse it?
What about 1'000 and other digit separators?
Which brace setting should I use?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.