In C (ANSI C) all variables are auto by default, so what's the reason for auto keyword? Can you name examples of usage auto keyword? (Except C++14).
-
3http://stackoverflow.com/questions/2192547/where-is-the-c-auto-keyword-used – Alexguitar Dec 13 '15 at 00:36
-
This is a possible [duplicate](http://stackoverflow.com/questions/4688816/concept-of-auto-keyword-in-c) question. – Michi Dec 13 '15 at 00:37
-
As you ask for C (btw. It's called ISO-C or just C because that should imply standard compliance), there is no need to exclude C++. C++ is a **diffferent language. Or do you also want to explicitly exclude Java, Ruby or Cobol, too? – too honest for this site Dec 13 '15 at 00:42
-
And not every variable has `auto` storage class by default. This can be found by a simple search or in any good C book or tutorial. – too honest for this site Dec 13 '15 at 00:45
-
Btw: What is the menaning in not so pure (i.e. dirty?) C? – too honest for this site Dec 13 '15 at 01:15
-
The duplicate is different in that it asks for use cases of `auto`, not for why it's in the language, it could have been left out. – fuz Dec 13 '15 at 09:49
2 Answers
In C
an auto keyword defines a local variable storage class that has a local or limited lifetime from the end of its declaration to the end of its enclosing scope (block or function); once program flow exit that scope, that instance of the variable ceases to exist.
Example::
Auto [data_type] [variable_name];
So this two:
auto int var;
int var;
Both the declarations has the same meaning. As auto keyword is the local lifetime is the default for local variables, auto keyword is extremely rarely used it’s only meaningful to a compiler-writer or interpreter developer making an entry in a symbol table or better readability auto keyword can be used. Auto keyword says this storage is automatically allocated on entering the block (as opposed to global static allocation, or dynamic allocation on the heap).

- 5,175
- 7
- 33
- 58
-
3
-
1Some programmers prefer to state intentions explicitly. The auto keyword permits that though few ever use it. In Java, for example, a package scope class member is declared when the public, private, and protected keywords are absent from a member declaration. I've never been comfortable with this design choice as it permits an oversight to cause an unintended visibility level. Probably the reason this never happens with C is that auto would be what is intended when a storage class is left out in a C variable declaration. – Ken Clement Dec 13 '15 at 00:48
-
1So you might want to fix that you "get it by default". YOu have the same fault as OP here. – too honest for this site Dec 13 '15 at 00:48
-
@KenClement: Please do not ad other languages in a comment to an answer. SO is no forum. And no, not every declaration is automatically `auto`. – too honest for this site Dec 13 '15 at 00:49
-
Also a C variable declared outside a function will be global if the static keyword is not specified - usually what is intended. – Ken Clement Dec 13 '15 at 00:49
-
1
-
1
-
-
@KenClement: 1) No it is not. Comments are to comment the answer, request clarification - with regard to the question. 2) `static` objects are very well global. They just have internal linkage. – too honest for this site Dec 13 '15 at 00:53
-
1@Olaf, You're being too pedantic. The auto keyword only works inside a function - a fact not yet mentioned - and is not valid outside a function. With what has already been stated one might assume (incorrectly) otherwise. – Ken Clement Dec 13 '15 at 00:56
-
@KenClement: "a C variable declared outside a function will be global if the static keyword is not specified" - there is nothing about `auto`. And that sentence is just confusing linkage and lifetime. Again: `static` variables are also global. It just makes them have internal linkage. Maybe you just have a look into the [standard](http://port70.net/~nsz/c/c11/n1570.html) – too honest for this site Dec 13 '15 at 01:14
-
@Olaf: "Global" is not defined by the standard, but the more usual usage of the term is for a variable that's visible and accessible throughout the entire program, which a static variable at file-scope is not (unless the entire program is in a single file, of course). – Crowman Dec 13 '15 at 01:22
-
@Olaf, In C static variables declared outside any function are static in storage class and have visibility only inside the compilation unit, i.e. the source file. Static variables declared inside a function are also static in storage class and have visibility only inside the function. It is only variables that are declared outside any function and lacking the static keyword that are "global" in visibility - i.e. visible across the entire program regardless of compilation unit. If such variables lack an initialization they are a declaration otherwise a definition. – Ken Clement Dec 13 '15 at 01:33
-
@PaulGriffiths: Not necedssarily. Normally, `global` is bound to the life-time and not external linkage. – too honest for this site Dec 13 '15 at 01:34
-
@Olaf: It's difficult to demonstrate what it or is not "normal", so there's no simple way to resolve the matter, but even the name "global" pretty clearly implies "available everywhere". `errno` is a global variable, for instance, and it wouldn't be if it had internal linkage. – Crowman Dec 13 '15 at 01:35
-
@KenClement: As `static` is a _storage class specifier_, it is no wonder it is a storage class. But as PaulGriffiths stated, the C standard does not even define the word "global" (but I don't agree with his definition of that word). Btw. I talked about linkage. Note also, `int I;` and `int i =1;` are **always** declarations. At file-scope the first is actually a _tentative definition_, too. Anyway, you have something to read; I'm out here. – too honest for this site Dec 13 '15 at 01:38
-
@Olaf, I'm not confusing "linkage" with "lifetime". In any event, I avoid using the term "linkage" altogether in this context since it is a very overloaded term, whereas "visibility" is not. I think it is time to put this to rest as we seem to have beaten this horse to death. – Ken Clement Dec 13 '15 at 01:39
-
The term linkage is quite well defined by the C standard. – too honest for this site Dec 13 '15 at 01:40
-
@Olaf, Paul is right about the conventional usage of the word "global". It refers to something visible across a fully composed program. – Ken Clement Dec 13 '15 at 01:41
All variables are not auto
by default; anything declared at file scope is static
, for example.
The auto
keyword is a holdover from the BCPL and B languages, from which C was derived. It is largely vestigial at this point, but by that same token it doesn't do any harm, so there's no reason to get rid of it, either.

- 119,563
- 19
- 122
- 198
-
An identifier in file scope has external linkage by default. `static` would change that to internal. The problem is that `static` / `auto` refer to both storage class as well as linkage. So, anything declared at file scope isn't `static` – Akay Jul 08 '20 at 20:04