为什么汽车A = 1;用C编译? [英] Why does auto a=1; compile in C?
问题描述
在code:
int main(void)
{
auto a=1;
return 0;
}
被编译而不被微软的Visual Studio 2012中的编译器,当文件有.c扩展名错误。我一直认为,当你使用.c扩展名,编译应根据C语法可以了,不是C ++。此外,据我所知没有一个类型的汽车被允许的仅在C ++中,因为C ++ 11,它意味着该类型从初始化推断出来。
gets compiled without errors by the MS Visual Studio 2012 compiler, when the file has the .c extension. I have always thought that when you use the .c extension, compilation should be according to the C syntax, and not C++. Moreover, as far as I know auto without a type is allowed only in C++ since C++11, where it means that the type is deduced from the initializer.
这是不是意味着我的编译器是不是坚持C,或者是code实际上是在C语言是否正确?
Does that mean that my compiler isn't sticking to C, or is the code actually correct in C-language?
推荐答案
汽车
是一个旧的C关键字,意思是本地范围。 汽车A
相同汽车int类型的
,因为本地范围是一个函数内声明的变量默认,这也是在本实施例相同 int类型的
auto
is an old C keyword that means "local scope". auto a
is the same as auto int a
, and because local scope is the default for a variable declared inside a function, it's also the same as int a
in this example.
此关键字实际上是从C的predecessor B,那里没有基本类型的后遗症:一切 INT
,指向 INT
,数组 INT
。(*)的声明是,要么汽车
或 EXTRN
[原文]。 C派生出一切为 INT
作为默认规则,所以你可以用
This keyword is actually a leftover from C's predecessor B, where there were no base types: everything was int
, pointer to int
, array of int
.(*) Declarations would be either auto
or extrn
[sic]. C inherited the "everything is int
" as a default rule, so you could declare integers with
auto a;
extern b;
static c;
ISO C摆脱了这一点,但很多编译器仍然接受它向后兼容。如果它似乎陌生,那么你应该认识到,相关的规则是在工作中
ISO C got rid of this, but many compilers still accept it for backward compatibility. If it seems unfamiliar, then you should realise that a related rule is at work in
unsigned d; // actually unsigned int
这仍然是常见于现代$ C $ C。
which is still common in modern code.
C ++ 11重复使用关键字,如果有C ++程序员用同本义这几年,它的类型推断。这主要是安全的,因为一切 INT
从C规则已经下降在C ++ 98;打破的唯一事情是自动T A
,没有人使用反正这是。 (某处在他的报纸上的语言的历史,在这个Stroustrup的意见,但现在我无法找到确切的参考。)
C++11 reused the keyword, which few if any C++ programmers were using with the original meaning, for its type inference. This is mostly safe because the "everything is int
" rule from C had already been dropped in C++98; the only thing that breaks is auto T a
, which no-one was using anyway. (Somewhere in his papers on the history of the language, Stroustrup comments on this, but I can't find the exact reference right now.)
(*)字符串B中的处理很有意思:你会使用 INT
的数组并在每个成员打包多个字符。乙实际上 BCPL 是用不同的语法。
(*) String handling in B was interesting: you'd use arrays of int
and pack multiple characters in each member. B was actually BCPL with different syntax.
这篇关于为什么汽车A = 1;用C编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!