This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Declaring automatic variables inside a while loop


On Tue, Jan 20, 2004 at 06:16:09PM -0500, Swaminathan Venkatakrishnaprasad wrote:
> Hello,

Hi.
 
> Could you please let me know if it is valid to declare an automatic
> variable inside a for loop.

What is an "automatic" variable? I never heard of such a thing. Can
you elaborate?!

> If yes, is it also allowed in ANSI standard (could you please refer
> me to the sec #).

However, I'd say that your code is valid ANSI C, though I don't have
the sec # at hand.

> GCC Version : 2.95.3
> Example:
> void f()
> {
>     int i;
>     i = 10;
>     while(i >=0 )
>     {
>        int j;
>        i=j;
>        printf("j=%d \n", j);
>        i--;
>     }
> return;
> }
> 
> Also, in the above example would the number of iterations contribute
> anything to the stack size since an automatic variable is declared
> inside the while loop??

You declare a *single* variable on the stack inside the while loop
named `j'. Which is not initialized by the way and that's why it's
undefined whether the loop terminates or not -- either it terminates
after a single iteration (j happens to be <= 0) or else it runs ad
nauseam.

Since you declare a *single* variable which is accessible inside the
scope of the while loop, the number of iterations has no impact on the
stack size.

HTH
-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux user                         - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]