Question from lee77 ):
- int main(void)
- {
- auto a = 1;
- return 0;
- }
Write the code shown above in a file with the extension. c. No error will be reported during compilation using the MS Visual Studio 2012 compiler. I always think that when you use the. c extension, the compiler will compile according to the C language syntax, rather than c ++. Moreover, as far as I know, the auto variable declaration that does not display the definition type is only allowed after the C ++ 11 standard, in C ++ 11, the meaning of auto is that the type of the variable can be deduced by the initialization program.
Does this mean that my compiler does not strictly abide by the C language standards, or is this Code actually correct in the C language?
The best answer is larsmans ):
Auto is an ancient keyword in C language. It represents "local scope ". 'Autoa' is equivalent to 'Auto int A', and the variable definition in the function is a local scope variable by default. In this example, it is also equivalent to 'int '.
This keyword (auto) is actually the predecessor of C language-the legacy of Language B. Language B has no basic types: All types are int, pointer to int, array of int. (*) Declarations of these types can only be 'auto' or 'extern '. C language inherits "any type is int" as the default rule, so you can declare an integer using the following method:
- auto a;
- extern b;
- static c;
The iso c Standard has abandoned this rule, but of course some compilers accept it for backward compatibility. If you still feel unfamiliar, you should understand that a similar rule takes effect in the following statement:
- unsigned d; // actually unsigned int
The above code is very common in modern code.
C ++ 11 reuses this keyword (auto). Since its original meaning is rarely used by C ++ programmers, It is reused in C ++ 11 for Type derivation. This is usually safe because the C language Rule "all types are int" has been abandoned by the C ++ 98 standard; the only unsafe method is 'Auto T A', but no one will do this. ()
(*) String processing in Language B is very interesting: You will use the int array and wrap multiple characters in each member of the int array. In fact, language B is BCPL (a variant of BCPL) of different syntaxes ).
Http://blog.jobbole.com/67040/.