C Beautifier

Rebuild the indentation of a C file from its brace structure, keeping preprocessor directives at the left margin.

C source
Formatted C
Runs locally in your browser

About the C Beautifier

There is no C compiler in a browser tab, so this beautifier does not pretend to be clang-format. It is a scanner that understands the parts of C that decide layout: block comments, line comments, string and character literals, preprocessor lines with backslash continuations, and the three bracket pairs. Everything it recognises as a token is copied out unchanged; only the whitespace between tokens is rewritten.

Indentation follows brace depth. A line that opens with a closing brace steps back one level, case and default labels sit one level inside their switch, and directives such as #include, #define and #ifdef are pinned to column zero the way most C style guides ask for. A semicolon inside the header of a for loop does not start a new line, because the scanner tracks parenthesis depth.

With Split statements onto new lines enabled, a file that arrived on one long line is broken after every brace and every top level semicolon. Disable it when the line breaks in the file are already deliberate and only the leading whitespace is wrong, for example after a bad merge or a paste from a PDF.

One honest limitation: nothing here reflows long expressions or aligns pointer stars, and macro bodies that hide braces can confuse the depth counter. For C++ classes with access labels and templates, use the C++ Beautifier instead.

How to use

  1. Paste a .c or .h file into the left pane, or press Sample for a short example with a struct and a loop.
  2. Choose Indent. The Linux kernel style is a tab; GNU style is 2 spaces.
  3. Keep Split statements onto new lines on for compressed source and off when you only want the leading whitespace fixed.
  4. Save the result with Download, which names the file formatted.c.

Common questions

Will it move my braces to the next line, K and R style or Allman style?
No brace is moved. An opening brace stays at the end of the line it was written on, so the brace style you already use survives the pass.
What happens to multi line macros?
A directive and every line joined to it by a trailing backslash is treated as one unit and printed at column zero, untouched.
Does it check that my C compiles?
It does not. Use a compiler for that. Unbalanced braces here simply produce indentation that stops making sense partway down, which is often a quick way to spot the missing brace.
Are comments reformatted?
Comment text is never edited. A block comment keeps its internal line breaks and each continuation line is aligned under the opening marker.