tags:

views:

1311

answers:

5

For the longest time I thought there was no reason to use the static keyword in C, because variables declared outside of block-scope were implicitly global. Then I discovered that declaring a variable as static within block-scope would give it permanent duration, and declaring it outside of block-scope (in program-scope) would give it file-scope (can only be accessed in that compilation unit).

So this leaves me with only one keyword that I (maybe) don't yet fully understand: The auto keyword. Is there some other meaning to it other than 'local variable?' Anything it does that isn't implicitly done for you wherever you may want to use it? How does an auto variable behave in program scope? What of a static auto variable in file-scope? Does this keyword have any purpose other than just existing for completeness?

+13  A: 

The auto keyword has no purpose at the moment. You're exactly right that it just restates the default storage class of a local variable, the really useful alternative being static.

It has a brand new meaning in C++0x. That gives you some idea of just how useless it was!

Daniel Earwicker
oh man, is that ever useless. I like the new meaning though. It makes some code a lot less verbose and redundant.
Carson Myers
Yes, having used the equivalent in C# it will probably make a huge difference. More so in C++ if you're using expression templates where the types are so complex that they were never intended to be written out by hand.
Daniel Earwicker
+13  A: 

auto is a storage class specifier, static, register and extern too. You can only use one of these four in a declaration.

Local variables (without static) have automatic storage duration, which means they live from the start of their definition until the end of their block. Putting auto in front of them is redundant since that is the default anyway.

I don't know of any reason to use it in C++. In old C versions that have the implicit int rule, you could use it to declare a variable

int main(void) { auto i = 1; }

To disambiguate from an assignment expression. But this doesn't work in C++ anyway (you have to specify a type). Funny enough, the C++ Standard writes:

An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default. [Note: hence, the auto specifier is almost always redundant and not often used; one use of auto is to distinguish a declaration-statement from an expression-statement (6.8) explicitly. —end note]

Which refers to the following scenario, which could be either a cast of a to int or the declaration of a variable a of type int having redundant parentheses around a. It is always taken to be a declaration, so auto wouldn't add anything useful here, but would for the human, instead. But then again, the human would be better off removing the redundant parentheses around a first of all.

int(a);

With the new meaning of auto arriving with C++0x, I would discourage using it with C++03's meaning in code.

Johannes Schaub - litb
I didn't know you could do that--which C is this? Pre-ANSI?
Carson Myers
It will work in the next standard release of C++. I never thought of auto i = 1. Most examples of the new usage use auto i = container.begin()
Zan Lynx
C++ compilers often used to have implicit int for return values from functions, back in the ARM days before the standard... Before the EMPIRE...
Daniel Earwicker
@Carson, it's standard in C89. The use of implicit-int however is obsolescent even there, and is banned from C99 too, and was never standard in C++.
Johannes Schaub - litb
Interesting, I didn't know about that little nuance with implicit-int.
Adam Rosenfield
I just recognized it as my compilers way of telling me I forgot to forward-declare a function. It would tell me that my usage of a function was different from the way it was declared because of implicit int.
Carson Myers
The best part is that programmers used to write "auto" (four letters) to save themselves from writing "int" (three letters).
Max Lybbert
@Max - hey, a lot of people say "double-u-double-u-double-u" as an abbreviation of "World Wide Web".
Daniel Earwicker
+1  A: 

"auto" supposedly tells the compiler to decide for itself where to put the variable (memory or register). Its analog is "register", which supposedly tells the compiler to try to keep it in a register. Modern compilers ignore both, so you should too.

T.E.D.
+4  A: 

GCC has a special use of auto for nested functions - see here.

If you have nested function that you want to call before its definition, you need to declare it with auto.

qrdl
this is a great, albeit compiler-dependent, implementation of auto. Thanks for the research :)
Carson Myers
A: 

In old compiler, auto was one way to declare a local variable at all. You can't declare local variables in old compilers like Turbo C without the auto keyword or some such.

chaz