Standard Indentation etc.

James Logan III logan at vsedev.VSE.COM
Sat Dec 17 12:46:34 AEST 1988


In article <5181 at bsu-cs.UUCP> dhesi at bsu-cs.UUCP (Rahul Dhesi) writes:
# Over-indentation eats up precious space.  Try this:
# 
# switch (expression) {
# case 'a':             /* lots of space here for a comment */
# 	statement
# 	...
# 	break;
# case 'b':
# 	statement
# 	...
# 	break;
# } /* switch */
# 
# Having each 'case' keyword aligned with the initial 'switch' keyword is
# similar to having 'else' aligned with 'if'.  We create a comb, and pack
# the C code in the gaps.

I agree.  I used to indent like this:

switch (exp) {
	case 'a':
		stmt;
		stmt;
		break
	case 'b':
		stmt;
		stmt;
		break;
}

Until I ran into problems when building finite-state machines
that required nested switch statements.  Once I stopped indenting
the "case" keywords I realized that I liked the way it looks
better too.

I see some alternate forms of indentation as inconsistent with
the way braces are used in function declarations.  An example
follows:   

func()
{
	for (;;)
	    {
		stmt;
		stmt;
	    }
}

I would probably use braces beneath and at the same level as the
"for", but when I learned C many years ago I got used to the K&R
style and have used it ever since.  

I do format long printf() statements strangely, but I think it
makes sense and is consistent with the usage of braces in C. 
This fprintf() doesn't have a particularly long string, but it
exemplifies my meaning:  

		if ((infd = fopen(filename, "w")) == (FILE *)NULL) {
			fprintf(
				stderr,
				"%s: cannot open ",
				myname
			);
			perror(filename);
			exit(1);
		}


-- 
Jim Logan		logan at vsedev.vse.com
(703) 892-0002		uucp:	..!uunet!vsedev!logan
			inet:	logan%vsedev.vse.com at uunet.uu.net



More information about the Comp.lang.c mailing list