C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Automatic (auto) variables in c language

The variables which are declared inside a block are known as automatic or local variables; these variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block.

These variables have local scope to that block only that means these can be accessed in which variable declared.

Keyword 'auto' may be used to declare automatic variable but we can declare these variable without using 'auto' keywords.






Consider the following declarations

    int main()
    {
	    auto int a;
	    int b;
	    ....
	    return 0;
    }

Here, both variables a and b are automatic variables.

Automatic variables in other user defined functions

An automatic or local variable can be declared in any user define function in the starting of the block.

Consider the following code

void myFunction(void)
{
	int x;
	float y;
	char z;
	...
}

int main()
{
	int a,b;
	myFunction();
	....
	return 0;
}

In this code snippet, variables x, y and z are the local/automatic variable of myFunction() function, while variables a and b are the local/automatic variables of main() function.


COMMENTS