ModernCalcs

C++ Formatter

Indent
#include <iostream>
#include <vector>
using namespace std;

class Stack {
  public:
  int top;
  vector<int> data;
  Stack() { top = -1; }
  void push(int x) {
    data.push_back(x);
    top++;
  }
  int pop() {
    if (top < 0) return -1;
    top--;
    return data.back();
  }
};

int main() {
  Stack s;
  s.push(10);
  s.push(20);
  cout << s.pop() << endl;
  return 0;
}

Preprocessor directives (#include, #define) are kept at column 0. For production code, use clang-format with a .clang-format config file.

C++ Formatter: Readable Indentation Without a Build Tool

Unformatted C++ code — generated by AI, minified, or pasted from Stack Overflow — is hard to read and hard to review. This formatter strips existing indentation and reapplies it based on brace depth, producing consistently indented C++ that matches the K&R and Allman style conventions used in most C++ codebases.

Formula
// Input: flat class Stack { public: int top; void push(int x) { data.push_back(x); } }; // Output: properly indented class Stack { public: int top; void push(int x) { data.push_back(x); } };

Preprocessor directives (#include, #define, #ifdef) are always kept at column 0 regardless of block depth.

Brace-Depth Indentation

C++ uses { and } to delimit all blocks — class bodies, function bodies, if/else, for, while, switch, and namespace blocks. The formatter tracks the running brace depth and applies it as the indentation level for each line. A line beginning with } is dedented before output; a line ending with { causes the next line to be indented.

Preprocessor Directives at Column 0

C/C++ preprocessor directives (#include, #define, #ifdef, #pragma, etc.) are processed by the preprocessor before compilation. They conventionally live at column 0 and are sensitive to whitespace. This formatter always outputs them at column 0, regardless of the surrounding block depth.

clang-format for Production Code

For code you are committing to a repository, clang-format is the standard tool. It reads a .clang-format config file that can enforce Google, LLVM, Microsoft, Mozilla, WebKit, or custom style. It also handles line-length limits, pointer alignment, and spacing rules that this browser-based tool does not.

Practical Examples

Formatting AI-Generated C++

AI code generators often produce flat C++ without indentation.

  • 1.Paste the AI-generated C++ code
  • 2.Choose 2 or 4 space indentation
  • 3.Copy the formatted output
  • 4.Review class members, function bodies, and nested blocks for correctness

What Gets Formatted

  • All { } blocks: classes, functions, if/else, loops, switch
  • Preprocessor directives kept at column 0
  • 2 or 4 space indentation toggle
  • Multiple blank lines collapsed to a single blank line

Good Use Cases

  • Formatting AI-generated C++ snippets
  • Making minified or obfuscated C++ readable
  • Quick review of pasted code before adding to a project
  • Teaching and explaining C++ code structure

Frequently Asked Questions

Why do #include and #define stay at column 0?

C++ preprocessor directives must start at column 0 (or optionally with whitespace, but column 0 is conventional). If a #include is indented, some strict compilers issue warnings. This formatter keeps all lines starting with # at column 0.

Does this handle templates and angle brackets?

Template angle brackets (<, >) are not used for indentation. Only curly braces {} determine block depth. Template declarations like template are treated as normal lines.

What about namespace blocks?

Namespace blocks use {} and are indented like any other block. Many style guides (Google C++ Style) recommend not indenting namespace contents — you can toggle that manually after formatting.

Should I use this for production code?

For quick inspection and readability. For production code committed to a repo, use clang-format with a .clang-format configuration file — it supports Google, LLVM, Microsoft, and Mozilla style guides precisely.