FormatterHQ

C Code Formatter

Format C source and headers with clang-format. Choose LLVM, GNU, Mozilla or another preset; set indent width, tabs and the column limit.

Input C
Output C
Ready.

How to use the C Formatter

  1. Paste your C source code into the Input editor, or use Sample, Upload file or Load URL.
  2. Pick a Style preset (default: LLVM), keep Indent on Style default or override it, and set the Column limit (0 means no limit).
  3. Press Format; with Auto update on, the output refreshes as you edit.
  4. Use Copy or Download above the output panel to take the formatted file with you.

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 if statements 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 (BasedOnStyle in a .clang-format file).
  • 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: #define bodies are formatted cautiously, and code inside #if 0 blocks 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.

Frequently asked questions

Which C style should I choose?
Match the project you are contributing to. For new code, LLVM or Google style are popular modern defaults; GNU projects use GNU style, and Windows code bases often use Microsoft style.
Can I use my own .clang-format file?
Not directly. The tool offers the seven built-in presets plus indent and column-limit overrides, which cover the most common settings.
Does it check that my C code compiles?
No. clang-format formats tokens and does not run a compiler. Syntax errors remain in the output, usually visible as odd indentation.
Will it change my pointer declarations?
Only their spacing. LLVM and GNU styles write int *p, while Google and Chromium write int* p. The meaning does not change.
Can I format header files?
Yes. Paste the contents of any .h file; declarations, include guards and macros are formatted like source files.

Last updated September 26, 2026

Related tools