Automatic Variables in C

auto variables are defined inside a function. A variable declared inside the function without storage class name is, by default, an auto variable. These functions are declared on the stack. The stack provides temporary storage. The scope of the variables is local to the block in which they are defined. These variables are available only to the current block or program. Once the executions of the function take place and the return turns off the function, the contents and existence of the auto variables or local variables vanish. Whenever a function is executed, all auto variables are allocated memory, and deallocated when the execution of function ends. The auto variables are safe, i.e. they cannot be accessed directly by other functions.

A keyword auto is used for declaration of automatic variables. By default, every variable pertains to auto storage class.

void main()
{
auto float p=5.5;
clrscr();
call2();
printf("\nP=%2.2f",p);
}
 
void call1()
{
auto float p=10.54;
printf("\nP=%2.2f",p);
}
 
 call2()
 {
 auto float p=20.50;
 call1();
 printf("\nP=%2.2f",p);
 }

Output

P=10.54
P=20.50
P=5.50

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.