Always use braces

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Dec 27 15:46:51 AEST 1988


In article <11037 at ulysses.homer.nj.att.com> cjc at ulysses.homer.nj.att.com (Chris Calabrese[mav]) writes:
>In article <271 at twwells.uucp>, bill at twwells.uucp (T. William Wells) writes:
>> In article <8 at estinc.UUCP> fnf at estinc.UUCP (Fred Fish) writes:
>> : ...  They were teaching her to write code like:
>> :       inchar = getchar ();
>> :       while (inchar != EOF)
>> :               if (inchar == 0)
>> :                       goto done;
>> :               else
>> :                       inchar = getchar ();
>> : done: return inchar;
>Any C guru qualified (in my humble opinion) to teach
>C would be trying to teach code which looks like:
>	do	{
>		inchar = getchar();
>		} while(inchar != EOF && inchar);
>	return inchar;

Here's mine.  The exact formatting is NOT IMPORTANT.  What DOES matter
is the comments and the fact that it is obvious how one gets each
possible return value.

	#include	<stdio.h>

	#define	MAGIC	'\0'	/* silly terminating character value */

	int			/* returns EOF or MAGIC (why??) */
	some_function( void )	/* ANSI C assumed; else delete "void" */
		{
		int	c;	/* character from standard input */

		while ( (c = getchar()) != EOF )
			if ( c == MAGIC )
				return MAGIC;

		return EOF;
		}



More information about the Comp.lang.c mailing list