A C formatter based on clang-format
This C formatter runs clang-format, the formatter maintained by the LLVM project and used by projects such as LLVM itself, Chromium and many embedded code bases. It is compiled to WebAssembly, so it formats your code locally in the browser with the same engine as the command-line tool. Pick a preset style, adjust indentation and the column limit, and paste in anything from a single function to a whole .c or .h file.
Example
#include <stdlib.h>
typedef struct node{int value;struct node*next;}node_t;
node_t*push(node_t*head,int v){node_t*n=malloc(sizeof *n);if(!n)return NULL;n->value=v;n->next=head;return n;}
static const char*names[]={"alpha","beta","gamma"};
With the LLVM preset:
#include <stdlib.h>
typedef struct node {
int value;
struct node *next;
} node_t;
node_t *push(node_t *head, int v) {
node_t *n = malloc(sizeof *n);
if (!n)
return NULL;
n->value = v;
n->next = head;
return n;
}
static const char *names[] = {"alpha", "beta", "gamma"};
LLVM style binds the * to the variable name (node_t *n), which reflects how C declarations actually parse: in int *a, b; only a is a pointer. The short if body moves to its own line, because LLVM style does not allow single-line if statements.
Brace and layout styles compared
The same function in three presets shows how much the style matters:
/* GNU */
int
max (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
/* Microsoft */
int max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
/* Mozilla */
int
max(int a, int b)
{
if (a > b) {
return a;
}
return b;
}
- GNU – return type on its own line, a space before the parenthesis of calls, braces indented by two spaces and their contents by two more. Used by GNU projects such as GCC and coreutils.
- Microsoft – Allman braces, 4-space indent.
- Mozilla – function braces on a new line, control-statement braces attached, return type on its own line.
- LLVM, Google, Chromium – K&R-style attached braces with 2-space indentation; Google allows short
ifstatements on one line. - WebKit – 4-space indent, function braces on a new line.
What about Linux kernel style?
The Linux kernel uses tabs displayed as 8 columns, an 80-column limit and function braces on their own line; it ships its own .clang-format file in the source tree. This page cannot load custom configuration files, but Tab indentation with the Mozilla or WebKit preset and a column limit of 80 gets you close for small snippets.
Options
- Style – the base configuration (
BasedOnStylein a.clang-formatfile). - Indent – Style default, 2–8 spaces, or tabs (indentation with tabs, 4 columns wide).
- Column limit – 100 by default; 0 means lines are never wrapped for length.
Things clang-format leaves alone
- Macros:
#definebodies are formatted cautiously, and code inside#if 0blocks is kept. - Include order: includes are sorted. With the LLVM preset this happens only within groups separated by blank lines; the Google preset also regroups them by category.
- Syntax errors: clang-format does not compile. A missing semicolon or brace does not stop it; the resulting indentation shows where things went wrong.
- Comments: kept as they are; overly long comments may be re-flowed to the column limit.
Working in C++? The C++ Formatter uses the same engine with C++ parsing rules. To see exactly what changed between your original and the formatted file, paste both into the Diff Checker. Formatting runs locally; your code is never uploaded.