Jump to content

Automatic variable

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 203.101.79.35 (talk) at 12:45, 15 April 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Automatic Variables: Variables local to a block are called automatic variables. They are automatically allocated on the stack when that block of code is entered. When the block exits, the variables are automatically deallocated.

for example, try the following code:

main()
{
  {
    int a;
    a = 10;
  }
  {
    int b;
    printf("b = %d\n", b);
  }
}

On most compilers (read gcc) this will give the output

b = 10

because the same memory location that was allocated to a is allocated to b when the second block is entered.

That reminds me. Automatic variables will have a garbage value when declared. Better initialize it with valid value before using it.