We ❤️ Open Source
A community education resource
Clean up your code with Indent
Explore how Indent makes code cleanup easy so you can contribute to any project.

If you ask a group of C programmers what coding style they prefer, you’re likely to hear a variety of different answers. Some prefer the “K&R” C style, learned from Kernighan and Ritchie’s books on the C programming language. Others may like the GNU coding style, and others might prefer the Linux kernel coding style, or the BSD coding style. The differences don’t end there; you’ll also find strong opinions on tabs versus spaces in code.
But you don’t have to become an expert in a particular coding style before you can contribute to an open source software project. Instead, you can write code the way you want to and use a tool to convert your personal coding style to match the other person’s preferred style.
Indenting your code
The original indent was written for BSD Unix in the early 1980s; GNU created a similar version in the late 1980s. Both operate the same and support the same “short style” options, like -in to set the indentation level to n spaces. The GNU version also provides “long style” options that use a double hyphen, like –indent-leveln to set the indentation.
Most Linux systems should provide the GNU version, which provides a long list of options (about 90) that give fine control over how statements should be separated, aligned, and indented. For example, programmers who prefer to use spaces might use the -as or –align-with-spaces option. If you want to “cuddle” the else statement between curly braces, add the -ce or –cuddle-else option. You can also align comments to a specific column, when comments are to the right of program statements, using the -cn or –comment-indentationn option.
But if you just want to match another popular coding style, indent also provides options for that, including -gnu for the GNU coding style, -kr to match the style in Kernighan and Ritchie’s original C programming books, -orig for original Berkeley style, and -linux for the Linux kernel coding style.
Read more: How I built a Markdown-to-HTML tool on a 5MB FreeDOS system
Common coding styles
These styles all differ in how they use spaces or tabs, indent levels, “cuddled” else statements, and other formatting. Let’s look at a sample program without any formatting, and process it through indent to transform the source into the different common styles. This program counts the numbers from 1 to 10, and prints a period after any number that is divisible by 3:
#include <stdio.h>
int main()
{
int i; /* iteration variable */
for (i=1;i<=10;i++) {
printf("%d",i);
if((i%3)==0){puts(".");}
else{putchar('\n');}
}
return 0;
}
The source code doesn’t use spaces or any indenting, but it is still a valid program. When compiled, the program generates a list of ten numbers, with periods after 3, 6, and 9:
$ gcc -Wall -o count count.c
$ ./count
1
2
3.
4
5
6.
7
8
9.
10
Let’s use indent to transform this program into a more readable style. I’ll use the -st or –standard-output option to write the reformatted program to the standard output; the default behavior for indent is to overwrite the original file with the reformatted source code.
If you learned C programming by reading Kernighan and Ritchie’s original books, you might be familiar with the “K&R” style. This indents each level at 4 spaces, but assumes tabs at every 8 spaces. I’ve sent the output through the sed program to convert tabs to 8 underscores, so you can see the tabs:
$ indent -st -kr count.c | sed -e 's/\t/________/g'
#include <stdio.h>
int main()
{
int i;________________________/* iteration variable */
for (i = 1; i <= 10; i++) {
________printf("%d", i);
________if ((i % 3) == 0) {
________ puts(".");
________} else {
________ putchar('\n');
________}
}
return 0;
}
The original BSD style is similar to “K&R” style, but aligns variables in declaration statements. This also uses tabs at 8 spaces, but not between the variable type and the variable name. As in the previous example, I’ve used sed to convert the tabs to a series of underscores so you can see them:
$ indent -st -orig count.c | sed -e 's/\t/________/g'
#include <stdio.h>
int
main()
{
int i;________________/* iteration variable */
for (i = 1; i <= 10; i++) {
________printf("%d", i);
________if ((i % 3) == 0) {
________ puts(".");
________} else {
________ putchar('\n');
________}
}
return 0;
}
The Linux kernel coding style also puts the opening curly brace on the same line as a for or if statement, uses tabs instead of spaces at each level, and “cuddles” the else between the curly braces. Again, I’ve used sed to convert the tabs to a series of 8 underscores, so you can see the tabs:
$ indent -st -linux count.c | sed -e 's/\t/________/g'
#include <stdio.h>
int main()
{
________int i;________________________/* iteration variable */
________for (i = 1; i <= 10; i++) {
________________printf("%d", i);
________________if ((i % 3) == 0) {
________________________puts(".");
________________} else {
________________________putchar('\n');
________________}
________}
________return 0;
}
The GNU coding style starts a new line for the curly braces around the for and if blocks, which puts the else on a line by itself. The function type and function name are also on separate lines. GNU adds two spaces at each indenting level, and tabs at every 8 spaces. As before, I’ve used sed to convert the tabs to underscores:
$ indent -st -gnu count.c | sed -e 's/\t/________/g'
#include <stdio.h>
int
main ()
{
int i;________________________/* iteration variable */
for (i = 1; i <= 10; i++)
{
printf ("%d", i);
if ((i % 3) == 0)
________{
________ puts (".");
________}
else
________{
________ putchar ('\n');
________}
}
return 0;
}
Read more: How to use the findfirst-findnext functions to expand wildcards into filenames
Create your own style
When I write sample programs for my articles, I prefer to use a coding style that makes it easy for readers to understand the code, without adding too much vertical space that would add unnecessary pages if the article were printed. Therefore, my preferred style is very similar to the “K&R” coding style, but without tabs and avoiding the “cuddled” else statement. I also prefer to align any comments at column 40 to the right of code statements, which works well for publishers who prefer a maximum of 70 or 80 columns for sample code.
Whenever I want to reformat a sample program to include it in an article, I use this command line:
$ indent -kr --no-tabs -c40 --dont-cuddle-else file.c
I’ve saved this in a Bash script called reindent, which effectively makes it a “wrapper” so I can easily reformat code using the same settings:
$ reindent count.c
$ cat count.c
#include <stdio.h>
int main()
{
int i; /* iteration variable */
for (i = 1; i <= 10; i++) {
printf("%d", i);
if ((i % 3) == 0) {
puts(".");
}
else {
putchar('\n');
}
}
return 0;
}
The indent program is a very useful tool to reformat a C program according to any formatting rules you prefer. Explore what indent can do for you by reading the manual page with man indent. Experiment by using -st or –standard-output so you can view what the reformatted code looks like without modifying your original code, then save the command line in a “wrapper” script so you can indent your code and keep your programs neat and tidy.
More from We Love Open Source
- How I built a Markdown-to-HTML tool on a 5MB FreeDOS system
- How to use the findfirst-findnext functions to expand wildcards into filenames
- Explore the five steps of the FreeDOS boot sequence
- A throwback experiment with Linux and Unix
- 5 FreeDOS editors I love
The opinions expressed on this website are those of each author, not of the author's employer or All Things Open/We Love Open Source.