How useful is keyword auto in C?

To keyword auto, defined by the C language, it is an ancient and seemingly useless keyword in the language. I know that in C your function is to define that it should be stored in the stack as a local variable whose life ends with the end of the scope in which it was declared. I also know what it does in C++, although this is not the case.

This keyword is really necessary and has some real utility for the language. It seems to me useless, since all the scope variables of C are stored in the stack, and only through the use of specific functions is it possible to make use of the heap.

I remember reading something about macros that might make Keyword auto necessary, but I don't know if that really has anything to do with my question.

Author: Maniero, 2017-04-30

2 answers

The keyword auto was included in the C language because it was the keyword for declaring local variables in the predecessor of C, a language B-and yes, I'm serious. Since B had no types, a definition of a variable needed to be preceded by either auto or extrn (equivalent to extern of C).

Since in the principle of C there was a need to port a quantity of Code B to C, the keyword auto was included to reduce the workload of these carriers (another feature was the possibility of specifying functions without a return type, such as main() { /* ... */ } and having them return int implicitly).

 2
Author: Wtrmute, 2017-05-03 20:41:35

None.

Ok, technically it indicates where the variable will be stored, just as static and extern can be used. Only it can only be used within function. And if you don't use anything inside the function the declared variable will be local, which is the same as auto indicates. So its practical utility is zero.

In fact in macro can be useful to prevent the variable from being declared as static, but if you create a variable in a macro, you may be abusing the resource.

If you want to maintain code compatibility with C++11 and up, then do not use. C++11 specified another use for this keyword allowing type inference.

That is, your assessment is correct.

 5
Author: Maniero, 2017-04-30 16:43:34