C pet peeve - (nf)

utzoo!decvax!microsof!uw-beave!cornell!vax135!ariel!orion!lime!houti!hogpc!houxz!ihnp4!ixn5c!inuxc!pur-ee!davy utzoo!decvax!microsof!uw-beave!cornell!vax135!ariel!orion!lime!houti!hogpc!houxz!ihnp4!ixn5c!inuxc!pur-ee!davy
Tue Mar 22 18:27:34 AEST 1983


#R:linus:-1644100:pur-ee:15500014:000:1021
pur-ee!davy    Mar 22 10:06:00 1983


	While we're talking pet peeves, mine has always been the fact that
"return" (and "sizeof") do not require parentheses.  While I'm not saying
that they should require them, consider the following:

	main() 
	{
		int i;

		for (i=0; i < 5; i++)
			foo(i);
	}
	foo(n) 
	int n;
	{
		if (n == 3)
			return		/* <--- Note I forgot the ';' */
		printf("foo...%d\n", n);
	}

Now, the obvious intention of this program is to produce the output:

		foo...0
		foo...1
		foo...2
		foo...4

But, instead, it produces the output:

		foo...3

This is because (for those of you who can't figure it out) the "return", since
there is no semicolon, will return the "value" of the printf.  Thus, the 
compiler compiles foo() as:

		foo(n)
		int n;
		{
			if (n == 3) 
				return(printf("foo...%d\n", n));
		}

I wish the compiler would print something in this case (a return without a
semicolon followed immediately by a newline) like:

	warning: 'return' statement possibly misinterpreted

Anybody agree with me?

--Dave Curry
pur-ee!davy




More information about the Comp.lang.c mailing list