开发者

Concept of "auto" keyword in c

Could you please give me the exact concept o开发者_如何转开发f the keyword auto in a C program.

When I went through the book Deep C secrets, I saw this quote:

The auto keyword is apparently useless. It is only meaningful to a compiler-writer making an entry in a symbol table. It says this storage is automatically allocated on entering the block (as opposed to global static allocation, or dynamic allocation on the heap). Auto is irrelevant to other programmers, since you get it by default.


auto isn't a datatype. It's a storage class specifier, like static. It's basically the opposite of static when used on local variables and indicates that the variable's lifetime is equal to its scope (for example: when it goes out of scope it is automatically destroyed).

You never need to specify auto as the only places you're allowed to use it it is also the default.


It might be useful in C89 where you have an implicit int rule.

void f() {
  a = 0; // syntax error
  auto b = 0; // valid: parsed as declaration of b as an int
}

But then, you can just write straight int instead of auto. C99 doesn't have an implicit int rule anymore. So I don't think auto has any real purpose anymore. It's "just the default" storage specifier.


You get the auto behaviour by default whenever you declare a variable for example - int i = 0; However you do the same by explicitly specifying auto int i = 0 which is not needed.


I find that quote quite questionable. The logical level of a compiler has nothing to do with the logical level of the compiled language, even when the two languages are the same. May be I'm poor on fantasy but I really can't imagine how having or not a certain keyword can be useful for a compiler but not for a general program, especially in "C" where you cannot directly manipulate keywords or any form of code anyway and you've to reflect everything on data because, in "C", code and data are two completely distinct concepts.

My wild guess is that auto was there originally because it wasn't optional but mandatory, later when the language evolved and it wasn't necessary any more it still remained because of backward compatibility reasons with existing C code.


As said auto is the default for variables in function scope in C. The only usage that I have had for the keyword is in macros. For a macro that does a variable declaration you might sometimes want to ensure that the variable is not declared static or in file scope. Here the auto keyword comes handy.

This was particularly useful for C++ where you could abuse the constructor/destructor mechanism to do scope bound resource management. Since the auto keyword is currently changing its meaning to something completely different in C++, this use is not possible any more.


I am sure you are familiar with storage class specifiers in C which are "extern", "static", "register" and "auto". I am not sure, but I think it is compiler dependent. You see, with respect to storage class specifiers, there is a rule. We cannot use multiple storage class specifiers for a variable. That is why static global variables cannot be externed. Therefore, they are known only to their file. When you go to your compiler setting, you can enable optimization flag for speed. one of the ways that compiler optimizes is, it looks for variables without storage class specifiers and then makes an assessment based on availability of cache memory and some other factors to see whether it should treat that variable using register specifier or not. Now, what if we want to optimize our code for speed while knowing that a specific variable in our program is not very important and we dont want compiler to even consider it as register. I though by putting auto, compiler will be unable to add register specifier to a variable since typing "register auto int a;" OR "auto register int a;" raises the error of using multiple storage class specifiers. To sum it up, I thought auto can prohibit compiler from treating a variable as register through optimization.

This theory did not work for GCC compiler however I have not tried other compilers.

Another possible answer is that C was developed in 1970 but it did not have any standards until 1989 when C89 came out. The non-standard C, may have used auto often but when C89 came out as the standard C, perhaps they needed to maintain compatibility with the non-standard C.


Quote from c-programming-simple-steps.com on c-auto

Practical usage None. Really, there is no reason to use the auto keyword in C. The only place where we can use it is when we create a variable within a function but… all variables created there are already auto.

Why the C auto exists? For backward compatibility. The predecessors of C used the auto to declare variables. Back in the days there was a lot of code that had to be ported to the new C language. Adding the keyword to the language was of help in that case.


Suppose I have a function returning some kind of convoluted pointer to a complex structure or, as I am developing a function that could be returning different structures, I would like in C to have:

auto myautoptr=This_Function_that_returns_some_pointer_type();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜