[net.lang.c] More C fun ...

brandx@ihtnt.UUCP (12/06/83)

Here's another "feature" of C (run under UN*X 5.0 and 4.1BSD):

junk(j)
  int j;
{
  int j=3;  /* duplicate declaration */

  return(j);
}

main()
{
    printf("<%d>\n",junk(5));
}

compiles fine and outputs:

<3>

brandx@ihtnt.UUCP (12/06/83)

Sorry, I guess that isn't a bug after all.  It's just confusing.
The local variable j is considered to be within a block (seperate
from the parameter j).

grunwald@uiuccsb.UUCP (12/09/83)

#R:ihtnt:-201000:uiuccsb:9000008:000:381
uiuccsb!grunwald    Dec  9 00:04:00 1983

This makes sense.
The function decleration is defined as

function name (parameters)
		decleration statements
		compound-statement

I thing that each compound-statement introduces a new scope. Thus, this is
equivilent to complaining about the following return "6":

woof(j)
int j;
{
	int j = 3;
	{
		int j = 6;
		return(j);
	}
	return(j);
}
main()
{
	printf(" = %d\n", woof(5));
}

woods@hao.UUCP (Greg Woods) (12/13/83)

   On our v6-hacked-to-look-like-v7 system (running on an 11/70), that "junk"
code does *not* compile. The output from our compiler is:

junk.c:4: j redeclared
junk.c:4: Declaration syntax

   Just out of curiosity, I plan to try it on our VAX 11/750 running 4.1aBSD.

		 GREG
-- 
{ucbvax!hplabs | allegra!nbires | decvax!brl-bmd | harpo!seismo | ihnp4!kpno}
       		        !hao!woods

stroyan@hp-dcd.UUCP (12/16/83)

#R:ihtnt:-201000:hp-dcd:18400004:000:250
hp-dcd!stroyan    Dec 12 19:36:00 1983


That's just variable declaration on the fly.

main ()
{
	junk(1);
}

junk (j)
{
int	j;
	j=2;
	{
	int	j;
		j=3;
		{
		int	j;
			j=4;
			printf ("%d ", j);
		}
		printf ("%d ", j);
	}
	printf ("%d\n", j);
}

prints 4 3 2.

Mike Stroyan
hpfcla!stroyan