Why can "auto a = 1;" be compiled in C?

Source: Internet
Author: User

Question from lee77 ):

 
 
  1. int main(void)  
  2. {  
  3.     auto a = 1;  
  4.     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:

 
 
  1. auto a;  
  2. extern b;  
  3. 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:

 
 
  1. 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/.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.