Indentation with GNU Indent
I prefer to use GNU indented code (with minor modifications). I normally let emacs indent my code for me. However, when I am using someone else's code, I usually have to run indent. Here is the command I use to indent with:

     # indent -gnu -nbc -nbfda -nce -ncs -npcs -nprs -npsl -nsaf -nsai -nsaw -nsob -nss file.c

Using this command will turn this ugly source file:

#include <stdio.h>

int main()
{
float x;

printf("Please enter a number: ");
scanf("%f", &x);

printf("Number: %f\n", x);

if(x>1)
{
if(x==3.141592)
{
printf("The number is approximately equal to pi.\n");
}
else if(x==2.718282)
{
printf("The number is approximately equal to e.\n");
}
}
else
{
printf("The number is not greater than 1.\n");
}

return 0;
}

Into this much better looking file:

#include <stdio.h>

int main()
{
  float x;

  printf("Please enter a number: ");
  scanf("%f", &x);

  printf("Number: %f\n", x);

  if(x > 1)
    {
      if(x == 3.141592)
	{
	  printf("The number is approximately equal to pi.\n");
	}
      else if(x == 2.718282)
	{
	  printf("The number is approximately equal to e.\n");
	}
    }
  else
    {
      printf("The number is not greater than 1.\n");
    }

  return 0;
}

Obviously, the indented source file is much easier to follow. Without some of the -n* options, some very stupid things will be done by indent. The datatypes in function declarations will get their own lines, there will be spaces after every ( or before every ), and for gets a space before the ( -- just to name a few. If you like this indentation style, please use it. It makes it easier to see what's going on with the code. Well, at least to the extent that indentation is able to do that.



Home - Michael Mazack - Privacy Policy
michael @ mazack . org
Visitor Map