Concept of "auto" keyword in C
Asked Answered
B

7

23

What is the exact concept of 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.

Bigod answered 14/1, 2011 at 7:23 Comment(4)
In C++0x auto has a completely different meaning though. en.wikipedia.org/wiki/C%2B%2B0x#Type_inferenceHassle
@ssg: C++ is not the same language as C. That has nothing to do with the question.Denationalize
That's why it's a comment Cody, not an answer.Hassle
Possible duplicate of Where is the C auto keyword used?Favata
G
29

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. When it goes out of scope it is "auto"-matically destroyed.

Note that auto in C is currently (ie: in C17 and earlier) not like auto in modern C++ -- it does not perform any type inference. However, it is possible that C23 will change auto to do a weaker form of type inference. The auto keyword is pretty useless today: you never need to specify it as the only places you're even allowed to are exactly the places where automatic storage is already the default behavior.

Greenbrier answered 14/1, 2011 at 7:41 Comment(3)
Are you sure “the variable’s lifetime is equal to its scope”? I’d thought the lifetime was equal to the function call. E.g., is int foo() {int *p; {int i = 42; p = &i} return *p;} non-conforming?Taneka
@jcsalomon The C90 spec, in §6.1.2.4, says this about automatic storage duration: "storage is guaranteed to be reserved for a new instance of such an object on each normal entry into the block with which it is associated ... Storage for the object is no longer guaranteed to be reserved when execution of the block ends in any way." I don't have a copy of the C99 spec, but it seems unlikely that they would have change this.Greenbrier
Apparently the "auto" keyword is a holdover from the B language. In B, everything was a machine word, no other types. Variables were simply declared static, extern, or auto. auto then became the default for C. princeton.edu/~hos/mike/transcripts/thompson.htmOrientalism
S
8

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.

Stomodaeum answered 14/1, 2011 at 7:28 Comment(1)
I don't understand how this would be useful, even in C89. It doesn't add anything. Whereever auto can be used, it is also the default, implied int type or not.Footage
M
4

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.

Menorca answered 14/1, 2011 at 7:29 Comment(0)
B
2

As said auto is the default for variables in block 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.

Bondage answered 14/1, 2011 at 7:38 Comment(2)
Re: "auto is the default for variables in function scope in C": C11: "A label name is the only kind of identifier that has function scope."Athapaskan
@pmor, thanks for finding this bug in a 12 year old answer ;-) Now corrected to the correct terminology, namely block scope.Bondage
C
2

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.

Conjunctive answered 14/1, 2011 at 7:38 Comment(0)
P
0

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.

Protestant answered 22/7, 2020 at 19:42 Comment(0)
V
0

2024


The meaning of auto changed in C23:

The meaning of the auto keyword was changed to cause type inference while also retaining its old meaning of a storage class specifier if used alongside a type. Unlike C++, C23 allows type inference only for object definitions (no inferring function return type or function parameter type).[28]

Valle answered 26/5 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.