Fog Creek Software
Discussion Board




Uses for the C keyword "Auto"

I have searched google and could not find any examples of uses for the C keyword auto which gives variables local lifetime scope.

Has anyone seen perhaps a contrived use for the keyword?


Wednesday, August 18, 2004

"auto" is the default for local variables - that's why you don't see it used.  The variable will go out of scope at the end of the block it is declared.

If you want to local variables to persist for the lifetime of the program, use "static".

See:  http://www.ssec.wisc.edu/~dglo/c_class/vstorage.html

One of the Matts
Wednesday, August 18, 2004

'auto' has no function. It is merely a hold-over from earlier version.

Mr Jack
Wednesday, August 18, 2004

It's used to make interesting variable names.  E.g.:

auto matic ;
auto erotic;
auto mobile;

etc etc. 

Snotnose
Wednesday, August 18, 2004

I understand what auto is and does.  I'm looking for a contrived use of the keyword.  In other words, I'm looking for a situation in which it has to be used, if such a situation exists.


Wednesday, August 18, 2004

No such situation exists.

Mr Jack
Wednesday, August 18, 2004

Historically you would have written:

int variable;

as:

auto int variable;

No need to do so anymore :-)

Pythonic
Wednesday, August 18, 2004

you could try "auto static x".

Alex
Wednesday, August 18, 2004

"... where it has to be used..."

Since it is the language/compiler default, it NEVER 'has to be used', unless you have some style guide that DEMANDS you explicitly assign this.  I don't even think there is a compiler switch that would let it NOT be the default.

This is one of the problems of C, that you can have a key concept like this ('auto' variables are put on the local stack) but the language default 'hides' this behavior.

It's just trying to be helpful, I suppose, but there are so many un-intended C side-effects (and C++ has them too) that it becomes very hard to know exactly what is going on as a newbie -- and you only gain experience with the side effects after they bite you a few times.

Still, that's job security, I suppose.

AllanL5
Wednesday, August 18, 2004

Here's a contrived use.  Say you want to use a macro to specify
the storage class of a var.  An undefined macro means the variable
is left undefined.

I said contrived, right?

#define STORECLASS static
vs.
#define STORECLASS auto
vs.
#define STORECLASS extern
vs.
#undef STORECLASS

makes a difference here:

#ifdef STORECLASS

STORECLASS int a;
STORECLASS int b;
STORECLASS float f;

#endif

and allows you to create all of these the same way in a somewhat self-documenting
fashion without introducing any special symbols to store as values to be test
with #if's in the preprocessor.

Thomas E. Kammeyer
Wednesday, August 18, 2004

But (at least for GNU cpp) you can equally well write

#define STORECLASS /**/

and it will behave just the same as if you defined it to be "auto".  So this isn't even a contrived case where "auto" is doing something that an empty statement can't do...

Iago
Wednesday, August 18, 2004

iago-- that's dangerous. better
#define STORECLASS
the /**/ may bite you if you use /* */ to comment out a block of code, even temporarily.

mb
Wednesday, August 18, 2004

Flame me if I'm wrong, but I thought it was a compiler hint for optimization, doesn't have any defined effect.

Tom H
Wednesday, August 18, 2004

auto doesn't optimize anything. The keyword you're thinking of is register.

Chris Nahr
Thursday, August 19, 2004

Slightly OT, but..
Sometime in the "future" (ie next C++ standard) it's not entirely unlikely (IIUC) that 'auto' may be reused to specify automatic type deduction. ex:

auto v = myObject.get_BigUglyType();
// typeof(v) == BigUglyType

(another name suggested is 'decl')

This would provide many(most?) of the dynamic typing benefits to C++ while still having strong typing.

CppBoy
Thursday, August 19, 2004

"auto doesn't optimize anything. The keyword you're thinking of is register. "

Yea, I remembered that about 30 seconds after I posted :^(

Tom H
Thursday, August 19, 2004

the "auto" keyword dates from a _very_ old era, when C compilers were a _lot_ simpler than today.

For example, C programs didn't execute with a stack.
Instead, every function call frame, and every local variable
was statically allocated in the data segment, even though
scope rules were already what they are today. By the way, this completely forbade recursive functions.

the "auto" keyword was used to indicate that the storage used by a variable could be re-used by another one if the compiler was clever enough to prove that no conflict could happen. It was only a hint.

Today, all C compilers are capable of performing this optimisation automatically by using a stack for function call frames and local variables, so the "auto" keyword is simply meaningless.

Anonymous
Thursday, August 19, 2004

*  Recent Topics

*  Fog Creek Home