Automatic variable

From Wikipedia, the free encyclopedia

Automatic variables are variables local to a block. 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);
  }
}

Using the gcc compiler 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.

Automatic variables will have an undefined value when declared, so it is good practice to initialize it with valid value before using it.

[edit] See also

In other languages