Declaration within a loop.

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Sat Sep 30 00:44:37 AEST 1989


In article <3362 at scolex.sco.COM>, seanf at sco.COM (Sean Fagan) writes:


|  	main() {
|  		int a;
|  		for (a=0; a< 100; a++)
|  		{
|  			int b = foo();
|  			printf ("b = %d\n", b);
|  		}
|  		return (0);
|  	}
|  
|  If 'b' is not created each time, then foo() is only called once.  However,
|  you will note that it *isn't* called just once.

  This is logical, but wrong. You are guaranteed that foo() will be
called each time through the loop, and that b will be initiallized, but
it is an implementation choice if some explicit action which could be
identified as "creating" b will be done each time or once.

  In most existing C compilers (I don't claim all) any explicit action
needed is taken once on entry to the procedure. As long as
initialization is done each time you can't tell from program behavior if
the allocation is done once or many time (not do I see any reason to care).
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list