As far as I can tell from 40+ years of C programming, including compiler work, the auto
keyword has been completely useless in C for 50 years.
To answer your precise question, Why is auto
keyword useful for compiler-writers in C? It isn't useful at all; C compiler writers are just required to parse it as a keyword and implement its semantics as a storage class specifier.
It seems to be a left over from B, the predecessor to the C language, developed by Ken Thompson and Dennis Ritchie at Bell Labs in the late sixties and early seventies. I have never used B and I doubt Peter, whom I met in 1984 at Inria, has either.
Before C23, auto
can only be used to specify automatic storage class for definitions in the scope of a function. This is the default, so auto
is fully redundant and as long as the type or another qualifier is specified, auto
can be removed. There isn't any case where it was needed, so its inclusion in the C Standard is only rooted in the early history of the C language.
auto
has been used in C++ since C++11 to enable type inference in variable definitions, with or without automatic storage, where the compiler detects the type from that of the initializer.
With the current trend pushing for convergence on a common subset for the C and C++ languages, new semantics have been attached to this keyword in C23 modelled after the C++ semantics, but more restricted:
6.7.1 Storage-class specifiers
auto
may appear with all the others except typedef
;
auto
shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer.
If auto
appears with another storage-class specifier, or if it appears in a declaration at file scope, it is ignored for the purposes of determining a storage duration of linkage. It then only indicates that the declared type may be inferred.
Type inference is specified as:
6.7.9 Type inference
Constraints
1 A declaration for which the type is inferred shall contain the storage-class specifier auto
.
Description
2 For such a declaration that is the definition of an object the init-declarator shall have one of the forms
direct-declarator = assignment-expression
direct-declarator = { assignment-expression }
direct-declarator = { assignment-expression , }
The declared type is the type of the assignment expression after lvalue, array to pointer or function to pointer conversion, additionally qualified by qualifiers and amended by attributes as they appear in the declaration specifiers, if any. If the direct declarator is not of the form identifier attribute-specifier-sequenceopt, possibly enclosed in balanced pairs of parentheses, the behavior is undefined.
Type inference is very useful in C++ because types can be very complex and almost impossible to specify in variable definitions, especially with templates. Conversely, using it in C is probably counter productive, lessening code readability and encouraging laziness and error prone practices. It was already bad enough to hide pointers behind typedefs, now you can hide them completely with the auto
keyword.
To finish on a less serious note, I remember seeing it used in tricky interview tests, where the candidate is asked to find why this code does not compile:
#include <stdio.h>
#include <string.h>
int main(void) {
char word[80];
int auto = 0;
while (scanf("%79s", word) == 1) {
if (!strcmp(word, "car")
|| !strcmp(word, "auto")
|| !strcmp(word, "automobile"))
auto++;
}
printf("cars: %d\n", auto);
return 0;
}