for != while

SCHEUTZOW,MICHAEL J gt6294b at gitpyr.UUCP
Thu Sep 4 03:08:35 AEST 1986


> >It's well known that the equivalence between for and while breaks down if
> >there's a "continue" statement.  Here's another case I just discovered:
> >
> >main() {
> >	char *foo = "outer";
> >	for (;; printf(foo),exit(0)) {
> >		char *foo = "inner";
> >	}
> >}
> >
> >This prints "outer" (vax SVR2 compiler), though the for-while equivalence
> >might lead one to expect "inner".
> 
> The point is that the scope of the inner 'foo' is the compound statement
> which is the body of the for statement.  So, quite rightly...
> 
> Kenneth R. Ballou                ...!ucbvax!brahms!ballou

It took me a few minutes to figure out what Ken was saying, and he is 
quite right.  The above is equivalent to:

main()
{
char *foo = "outer";

    for (;;)
        {
            {
	    char *foo = "inner"; /* note the 'char' */
            }
        printf(foo);
        exit(0);
	}
}

Mike Scheutzow         "I _think_ these are my opinions;
Georgia Tech           let me ask my boss..."
gt6294b at gitpyr.uucp



More information about the Comp.lang.c mailing list