Seven Original Sins of K&R (Long)
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Fri Sep 28 14:14:12 AEST 1990
In article <4700066 at m.cs.uiuc.edu>, gillies at m.cs.uiuc.edu writes:
> Hey, how else can I write the following amazingly convoluted code
> (idea courtest of Harbison & Steele's book, first edition):
> main()
> {
> int x,i;
> x=1;
> switch(x) {
> case 1:
> for (i=0; i < 10; i++)
> case 2:
> printf("%d ",i);
> }
> }
Easily:
main()
{
int i;
for (i = 0; i < 10; i++) printf("%d ", i);
exit(0); /* you shouldn't leave this out */
}
This was possible because of the assignment x=1. If that assignment
had been x=2, the effect would have been undefined because i was not
initialised. That's a good reason not to jump into loops even if C lets you.
--
Fixed in the next release.
More information about the Comp.lang.c
mailing list