Wednesday 23 December 2009

Tip of the Day: Format C Code Using Indent

Indent is a powerful application developed by the GNU project, designed to format and change the appearance of C code. Indent will insert or delete whitespace characters to make your C code easier to read. It supports several code styles, including:
- GNU indent style (which is the default, or indent -gnu)
- Kernighan & Ritchie indent style (invoked with indent -kr)
- original Berkeley indent style (invoked with indent -orig)

Thus, to indent a C source file using the Kernighan & Ritchie style, one would use:

indent -kr source.c

Have a look at the screenshots below:

Original, wrongfully formatted source code

Same code, formatted using indent source.c (using default GNU style)

Same code, formatted with the Kernighan & Ritchie style (with indent -kr source.c)

Notice that indent is developed to work with C code, not C++; also, it will try to cope with invalid code too.

No comments: