Standard indentation?

Frans van Otten fransvo at htsa
Tue Dec 6 20:27:14 AEST 1988


In article <NELSON.88Dec5142722 at sun.soe.clarkson.edu> nelson at clutx.clarkson.edu writes:
>I know, this is a religious question.  However, I would like to know if
>anyone has codified a standard for indentation of C programs?

There seems to be a de-facto standard, to which I stronly disagree. I'll
just explain my style:

#define then    /*  as white space; see below  */

int func(a, b, c)
  int a;
  char b;
  double c;
  { if (c > 3.0e10) printf("big\n");
    while (a > 2)
      { putchar(b); putchar(c);
	if (a > 10)
	  then putchar('1');
	  else putchar('2');
      }
  }

1. then must be defined (as white space) because the then-action
   must be at the same level as the else-action. Some people dis-
   agree with this, but look at it this way: *every* if-statement
   can be rewritten into a switch-statement, and in that case, the
   then-action and the else-action are at the same level:

     if (expr)            switch (expr)
       then stat1;          { default: stat1; break;
       else stat2;            case 0:  stat2; break;
			    }

   For shortness, if there is no else-action, you can always write
   the then-part (with or without the 'then') on the same line as
   the if (expr). This doesn't make the program less readable.

2. Indentation always by two spaces. A tab uses too much space.

3. Closing } always straight below opening {. It's easier to see
   where the compound-statement ends.

4. Main rule: something *within* something else *always* indented.
   You can easily see what belongs to what, which is the next state-
   ment, etc.

I have been using this style for some years now. It has proved to me
that it is far more readable then any other style I ever saw. It has
proved useful debugging programs.
-- 
                         Frans van Otten
                         Algemene Hogeschool Amsterdam
			 Technische en Maritieme Faculteit
                         fransvo at htsa.uucp



More information about the Comp.lang.c mailing list