Tooling for C: Astyle

Home · Blog

14 June 2025

Part of a series: Tooling for C

Astyle is a source code formatter for C, C++, and several other languages. You run it on the command line and it “cleans up” your code, adding and removing whitespace for a uniform look. Formatting code automatically has become standard in some languages ever since Go came out with gofmt as its formatter. In C, there isn’t one standard style, so astyle has lots of configuration options that let you adapt it to your project’s conventions.

Astyle is pretty old – version 1.0 was released in 1998 – but it’s still in active development. The name stands for “artistic style”… which I guess is a bit of a joke. Cleaning up whitespace isn’t exactly art, right?

Installation

I initially installed astyle with the standard package manager on my Linux system (sudo apt install astyle on Debian), but then I realized that installed an older version that’s missing some features I wanted, so I compiled it from source instead. On Linux, that’s pretty easy:

  1. Download the source from the Releases page and unpack it.
  2. Compile it with cmake . && make. If you don’t have cmake, it should be easy to get with your distribution’s package manager.
  3. The compiled program will be AStyle/astyle. Copy that file to /usr/local/bin or some other location in your $PATH.

Use

There’s three ways to run astyle on the command line. The basic one is to give it one or more file names.

astyle file.c

will re-format file.c and, if it made any changes, save the original as file.c.orig. If you want it to just re-format your code and discard the original files, run it with -n, for example

astyle -n *.c *.h

Finally, if you run it without a filename, it’ll read code on standard in and write the formatted code on standard out. This is useful for editor integration. I’ve set up my editor so it sends the whole file through astyle before saving so I never really need to run astyle manually.

Configuration

You can set formatting options either on the command line or in a config file. For example,

astyle --indent=tab --pad-oper *.c

tells it to format your files using tabs instead of spaces and to add spaces around operators.

Once you’ve figured out the settings you want to use, you’ll probably want to put them in a configuration file. With astyle, you can set either a “project option file” (typically committed to version control with the project’s source code) or a “default option file” (typically in your home directory).

This brings us to the one really annoying thing about astyle: the rules for where it looks for options files are really confusing. You can read up on them in the documentation if want the details (see Option Files), but here’s what worked for me:

Formatting

By default, astyle only changes indentation and brace style (where { } go in function and type definitions). You’ll want to set at least the –style and –indent options to specify how. For example, with

astyle --style=style=attach --indent=tab

functions will be formatted like this:

int my_function() {
    return 1;
}

You can put the same options (with or without the “–”) in an options file. The following is a valid options file:

style=attach
indent=tab

By adding more options, you can get it to also change how expressions and control-flow constructs are formatted. Here’s the configuration I’m using:

style=attach
indent=tab
pad-oper
pad-header
unpad-paren
align-pointer=middle
remove-braces
attach-return-type
attach-return-type-decl
convert-tabs

That’ll format this code:

int
foo(int *);

int
foo(int *a)
  {
    if( !a ) {
        return 0;
    }
    return *a+1;
  }

to this:

int foo(int *);

int foo(int * a) {
    if (!a)
        return 0;
    return *a + 1;
}

If that looks perfect to you, go ahead and steal my config! In the more likely case that you disagree with some of my choices, you can see all options in the documentation.

Alternatives

To conclude, I want to quickly mention some other code formatters for C. I haven’t tried all of them – fiddling with four different code formatters just didn’t seem like the best use of my time – but these are the ones I’ve seen people recommend: