C Formatter

Format C with clang-format.

Runs locallyYour input is processed in this tab. The editor blocks outgoing network requests.
C input

The C Formatter runs clang-format, compiled to WebAssembly, on your C source and headers. Choose one of seven built-in styles — LLVM, Google, Chromium, Mozilla, WebKit, Microsoft or GNU — to match a project's conventions. It suits embedded, systems and student code that needs consistent layout before review. Everything runs in your browser — nothing is uploaded.

How to format C code

  1. Paste your code, open a .c or .h file with ⌘/Ctrl O, or load an example.
  2. Pick a Style. LLVM is the default; the output updates as soon as you change it, so you can compare styles quickly.
  3. Copy the result with ⌥/Alt C, download it with ⌘/Ctrl S, or choose Switch to C++ for C++ sources.

Examples

Functions in LLVM style

Everything on a few lines is expanded into two-space blocks with attached braces and spaces around operators.

C
#include <stdio.h>
int sum(int *a,int n){int s=0;for(int i=0;i<n;i++){s+=a[i];}return s;}
int main(void){int a[]={1,2,3,4};printf("%d\n",sum(a,4));return 0;}

Result:

C
#include <stdio.h>
int sum(int *a, int n) {
  int s = 0;
  for (int i = 0; i < n; i++) {
    s += a[i];
  }
  return s;
}
int main(void) {
  int a[] = {1, 2, 3, 4};
  printf("%d\n", sum(a, 4));
  return 0;
}

The same code in GNU style

GNU style puts the return type on its own line, indents braces by two spaces, and adds a space before every opening parenthesis.

C
#include <stdio.h>
int sum(int *a,int n){int s=0;for(int i=0;i<n;i++){s+=a[i];}return s;}
int main(void){int a[]={1,2,3,4};printf("%d\n",sum(a,4));return 0;}

Result:

C
#include <stdio.h>
int
sum (int *a, int n)
{
  int s = 0;
  for (int i = 0; i < n; i++)
    {
      s += a[i];
    }
  return s;
}
int
main (void)
{
  int a[] = { 1, 2, 3, 4 };
  printf ("%d\n", sum (a, 4));
  return 0;
}

Includes, a macro and an enum in Google style

Google style sorts includes and moves system headers above project headers in their own group. Short enums stay on one line.

C
#include "config.h"
#include <string.h>
#include <stdlib.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
enum color{RED,GREEN,BLUE};
/* clamp v into [lo, hi] */
static int clamp(int v,int lo,int hi){if(v<lo)return lo;else if(v>hi)return hi;return v;}

Result:

C
#include <stdlib.h>
#include <string.h>

#include "config.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
enum color { RED, GREEN, BLUE };
/* clamp v into [lo, hi] */
static int clamp(int v, int lo, int hi) {
  if (v < lo)
    return lo;
  else if (v > hi)
    return hi;
  return v;
}

Options explained

  • Style — selects one of clang-format's predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft or GNU), which sets indentation, brace placement, pointer alignment, line limit and include ordering together.

Common C formatting problems

clang-format works on tokens rather than compiling the code, so it almost never rejects input. Problems show up as unexpected layout instead of an error message.

The output is indented strangely after a certain point

Usually a brace is missing. clang-format does not report unbalanced braces; it formats what it sees, so everything after an unclosed { stays one level deeper. Check the function where the indentation first drifts.

My include order changed

Every style sorts #include lines alphabetically within each block, and Google and Chromium also regroup system and project headers. If a header must come first, separate it from the rest with a blank line, which starts a new block that is sorted on its own.

Macros gained long runs of spaces and backslashes

Multi-line macros are reformatted and their line-continuation backslashes are aligned to the right edge of the line limit. This is standard clang-format behaviour; wrap the macro in // clang-format off and // clang-format on if you want to keep it as written.

A hand-aligned table was reflowed

Lookup tables and matrices lose their alignment because clang-format repacks initialiser lists. Surround them with // clang-format off and // clang-format on comments to protect them.

Text that is not C comes back unchanged

Input that is not recognisable as C is returned with little or no change and no error. Make sure you pasted source code, not a compiler log or assembly listing.

FAQ

Is this the real clang-format?

Yes. It is clang-format from the LLVM project compiled to WebAssembly, so the predefined styles behave as they do on the command line with --style=LLVM, --style=Google and so on. The formatter reads only the style you choose; a .clang-format file in your project is not read, and individual options such as IndentWidth cannot be overridden here.

Is my source code uploaded?

No. clang-format runs as WebAssembly inside your browser, and the editor is an isolated frame that blocks network requests, so nothing is uploaded or stored on a server. The selected style is remembered on this device. Your code is kept only if you turn on "Remember my last input", which is off by default.

Which style should I choose for Linux kernel code?

None of the presets matches the kernel exactly; kernel style uses eight-column tabs and has its own .clang-format file in the source tree. Of the built-in styles, LLVM gives the closest brace placement for control statements, but the indentation will differ. For kernel patches, run the kernel's own configuration locally and check with checkpatch.pl.

Does clang-format check my code for errors?

No. It is a formatter, not a compiler or linter. Syntax errors, undeclared identifiers and type mismatches are not reported, and unbalanced braces are formatted rather than rejected. Compile with warnings enabled, or use a static analyser, to find real bugs after formatting.

What is the difference between the C and C++ Formatter?

Both run the same clang-format engine with the same seven styles. The C Formatter accepts .c and .h files and passes a C file name to clang-format; the C++ Formatter accepts .cpp, .cc, .hpp and similar. For the same code and style the output is the same, so choose the one that matches your file.

How do I stop clang-format touching part of a file?

Put // clang-format off on the line before the region and // clang-format on after it. Everything between those comments is kept exactly as written, while code after the second comment is formatted normally. This is the usual way to protect register maps, lookup tables and carefully aligned initialisers.

For other languages, see the Go Formatter, the Java Formatter and the rest of the code tools.

clang-format styles compared

Style Indent Line limit Function braces Pointer Notable
LLVM 2 80 Same line int *p case labels flush with switch
Google 2 80 Same line int* p Regroups includes, indents case labels
Chromium 2 80 Same line int* p One parameter per line when wrapping
Mozilla 2 80 Next line int* p Return type on its own line
WebKit 4 None Next line int* p Never wraps long lines
Microsoft 4 120 Next line int *p All braces on their own line
GNU 2 79 Next line, indented int *p Space before (

Browse all Code Formatters