why are we going for auto-type storage class?

90 views Asked by At

In C, why we go for storage classes? I heard that auto type is the same as the local variable. in that case, why we go for auto type?is there any special by using auto type instead of local variables which is not mentioned as auto. for example,

int a=10; 

and

auto int a=10; 

both are stored in stack segment and scope of these variables is within the function.so what is the difference between these two? why we go for auto-type?

1

There are 1 answers

2
Sourav Ghosh On

First of all, auto (keyword) is not a type specifier, it's a storage class specifier. Quoting from the standard,

An object has a storage duration that determines its lifetime. There are four storage durations: static, thread, automatic, and allocated.

To quote C11, chapter ยง6.2.4, Storage durations of objects

An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration,[..]

and, regarding the linkage, (emphasis mine)

The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.

So, local variables, satisfying above conditions are by default, having automatic storage duration. You don't have to explicitly specify the auto keyword.

OTOH, type-specifiers determine the type (of data or variables). Going by the standard definition of type

The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it.