Declaring variables mid-function

bowman bowman@montana.com
Sat Dec 18 16:11:00 GMT 1999


Jamie Walker <jamie@sagaxis.co.uk> wrote:

>In standard C, you cannot declare variables part-way through any
>function.

I won't stick my neck out and say it is standard, as I don't have
Harbison & Steele handy, but gcc will accept declarations at the
head of any block. So

int foo() 
{
	int bar;
	bar = 1234;
	{
		int fubar;
		for (fubar=0; fubar<bar; fubar++)
			printf("completely fooed\n");
	}
}

should compile. I believe that fubar is at least lexically visible to the
end of the function, unlike some languages where the scope would be strictly
the block.

Comments from the language lawyers are welcome on what exactly the standard
stipulates are welcome.




More information about the Gcc-help mailing list