Re: Declaring automatic variables inside a while loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux