TO C OR NOT TO *C

Tom Stockfisch tps at chem.ucsd.edu
Tue Oct 24 11:01:46 AEST 1989


In article <3775 at condict.cs.vu.nl> condict at cs.vu.nl (Michael Condict) writes:
>	Arbitrarily_complicated_stuff_that_does_some_input
>	while (! feof(stdin)) {
>		.. do stuff with input ..
>		Arbitrarily_complicated_stuff_that_does_some_input
>	}

Someone proposed a language extension which handles this very elegantly:

	do
	{
		c =	getc(stdin);
	} while (!feof(stdin))
	{
		.. do stuff with input ..
	}

where the second compound statement is executed only when the "while" test
succeeds.  When it fails, execution continues after the second compound
statement.  Until such time as this makes it into a compiler, I just
use
	
	for (;;)
	{
		c =	getc(stdin);
		if (feof(stdin))	/* LOOP TEST */
			break;
		.. do stuff with input ..
	}

I always mark the loop test with an eye-catching comment.  Structured
programmers please note that there still is only one entrance point
and one exit point.

>This problem can be gotten around in C, using the ',' operator:
>
>	while (c = getc(stdin), !feof(stdin)) {
>		.. do stuff with c ..
>	}

This doesn't always work:  for instance, if the first part is a statement.
On all compilers I work on, there is the additional requirement (the
standard not withstanding) that
all operands of the comma operator be non-void.
-- 

|| Tom Stockfisch, UCSD Chemistry	tps at chem.ucsd.edu



More information about the Comp.lang.c mailing list