跳到主要内容

C++ 代码格式化工具

通过跟踪大括号({ })的嵌套深度来重新缩进 C 和 C++ 源代码——每个代码块获得一致的缩进,注释和字符串字面量保持不变。

输入

Drop a file or browse
One file · text

格式化选项

输出

格式化后的代码
 
这对您有帮助吗?

使用此工具的更多方式

REST API

curl -X POST https://api.iotools.cloud/v1/tool/cpp-code-formatter \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputCode": "int max(int a, int b) {\n        if (a > b) {\nret…",
    "indentSize": "4"
  }'

替换成您账户中的密钥。工具的字段即为请求体——没有额外包装。

让 AI 代理执行

Use the IOTools `cpp-code-formatter` tool (C++ Code Formatter) on this input:

YOUR_INPUT_HERE

将此粘贴给任何已连接 IOTools MCP 服务器的代理,再加上您的输入内容。

嵌入式小组件

<iframe
  src="https://iotools.cloud/embed/cpp-code-formatter/"
  width="100%" height="520" frameborder="0" scrolling="no" loading="lazy"
  title="C++ 代码格式化工具 — iotools.cloud"
  sandbox="allow-scripts allow-forms allow-same-origin allow-downloads allow-popups allow-popups-to-escape-sandbox"
  allow="clipboard-write"
  style="width:100%;border:1px solid #e5e7eb;border-radius:12px;overflow:hidden"></iframe>
<script src="https://iotools.cloud/embed.js" async></script>

把它放到您自己的页面上——免费,无需密钥,只需保留一个反向链接。

每次 API/MCP 调用费用5 积分起
需要更多积分?查看定价

也可通过

使用指南

What this tool does

C and C++ source picked up from a forum post, a diff, an AI-generated snippet, or a file that's been through one too many hand edits tends to end up with inconsistent indentation — tabs mixed with spaces, if bodies at the wrong depth, closing braces that don't line up with the line that opened them. This tool re-indents that code by tracking brace ({ / }) nesting depth line by line: every line is reset to depth × indent-size spaces, and a closing } is dedented before it's printed so it lines up cleanly with its opening brace.

It is a brace-depth re-indenter, not a full C++ formatter. It does not parse C++ grammar, does not rewrite operator spacing, does not enforce a brace style (Allman vs. attached), does not align pointers (int* p vs int *p), and does not wrap long lines to a column limit — the kind of things a real tool like clang-format does. What it fixes is the single most common readability problem: lines sitting at the wrong indentation level. Everything else about a line — the tokens, the spacing between them, comment wording — is left exactly as you wrote it; only the leading whitespace changes.

How to use it

  1. Paste your C or C++ code into the C / C++ Code box, or upload a source file directly.
  2. Choose an Indent Size — 2 or 4 spaces per nesting level (4 is the default and matches common C++ style guides).
  3. The re-indented result appears instantly in the Formatted Code panel. Copy it or download it as formatted.cpp.

Formatting happens automatically as you type or edit the input — there's no button to click.

How the indentation logic works

The formatter reads your code line by line and keeps a running nesting depth. A line is scanned for net {/} count: each { increases the depth that applies to lines after it, each } decreases the depth. A line whose first character is } is dedented one level before it's printed, so } (and } else {) line up visually with the line that opened the block.

Because indentation should not be thrown off by a stray brace that isn't really code, the scanner skips brace characters that appear inside "..." string literals, '...' character literals, // line comments, and /* ... */ block comments — including block comments that span multiple lines, during which depth tracking is frozen until the closing */ is found. This covers the overwhelming majority of real-world C/C++ source. A handful of edge cases are intentionally out of scope for this lightweight approach: a genuine C++ lexer would also need to handle raw string literals (R"(...)"), multiple closing braces at the start of one line (each gets treated as a single dedent step), and preprocessor macros that redefine brace-like tokens.

If braces in your input are unbalanced — a missing }, or an extra one — the formatter never errors or gets stuck. Depth simply stops decreasing once it reaches zero, so the rest of the file still formats sensibly instead of collapsing to negative indentation.

FAQ

Does this behave like clang-format? No. clang-format fully parses C++ and can rewrite brace style, operator spacing, pointer alignment, and line wrapping to match a named style (LLVM, Google, WebKit, etc.). This tool only fixes indentation depth based on brace nesting — a much smaller, but still very useful, slice of what makes code readable.

Will it change my code logic or rewrite tokens? No. Only the leading whitespace of each line is replaced. Everything else — spacing inside a line, string contents, comment text, variable names — is left exactly as written.

Does it work with plain C, not just C++? Yes. The formatter only looks at brace nesting and comment/string syntax, both of which are identical in C and C++.

What happens with unbalanced braces? The depth counter is clamped at zero — it never goes negative — so a missing or extra } degrades gracefully instead of producing garbled output for the rest of the file.

Can it format multiple files at once? Use the tool's batch mode (where available) to run the same indentation settings across a queue of uploaded files.

Privacy

This tool runs entirely in your browser. Your source code — including any comments, strings, or file paths it contains — is never uploaded to a server.

c++cppccode formatterindentbeautifybrace

喜欢这些工具?去掉广告吧。

一次性付款即可永久移除您账户中的所有广告。无需订阅,不追踪。