C自动关键字在哪里使用?

时间:2021-02-03 15:28:34

In my college days I read about the auto keyword and in the course of time I actually forgot what it is. It is defined as:

在大学的日子里,我读到关于auto关键字的文章,在这段时间里,我实际上忘记了它是什么。它被定义为:

defines a local variable as having a local lifetime

定义本地变量作为本地生命周期。

I never found it is being used anywhere, is it really used and if so then where is it used and in which cases?

我从来没有发现它在任何地方被使用,它真的被使用了吗?如果是这样的话,它在哪里使用,在哪些情况下?

8 个解决方案

#1


64  

auto is a modifier like static. It defines the storage class of a variable. However, since the default for local variables is auto, you don't normally need to manually specify it.

auto是一个静态的修饰语。它定义了一个变量的存储类。但是,由于本地变量的默认值是auto,所以通常不需要手动指定它。

This page lists different storage classes in C.

此页面列出了C中的不同存储类。

#2


97  

If you'd read the IAQ (Infrequently Asked Questions) list, you'd know that auto is useful primarily to define or declare a vehicle:

如果你阅读了IAQ(不常见的问题)列表,你就会知道auto主要用于定义或声明车辆:

auto my_car;

A vehicle that's consistently parked outdoors:

一辆一直停在户外的车:

extern auto my_car;

For those who lack any sense of humor and want "just the facts Ma'am": the short answer is that there's never any reason to use auto at all. The only time you're allowed to use auto is with a variable that already has auto storage class, so you're just specifying something that would happen anyway. Attempting to use auto on any variable that doesn't have the auto storage class already will result in the compiler rejecting your code. I suppose if you want to get technical, your implementation doesn't have to be a compiler (but it is) and it can theoretically continue to compile the code after issuing a diagnostic (but it won't).

对于那些缺乏幽默感并且想要“仅仅是事实”的人来说,简单的回答是根本没有理由使用汽车。允许使用auto的唯一时间是有一个已经有自动存储类的变量,所以您只是指定了会发生的事情。试图在没有自动存储类的任何变量上使用auto将导致编译器拒绝您的代码。我想,如果您希望获得技术,您的实现不必是一个编译器(但它是),而且理论上它可以在发布诊断(但它不会)之后继续编译代码。

Small addendum by kaz:

小由kaz附录:

There is also:

还有:

static auto my_car;

which requires a diagnostic according to ISO C. This is correct, because it declares that the car is broken down. The diagnostic is free of charge, but turning off the dashboard light will cost you eighty dollars. (Twenty or less, if you purchase your own USB dongle for on-board diagnostics from eBay).

这需要根据ISO来诊断,这是正确的,因为它声明车坏了。诊断是免费的,但是关闭仪表板灯将花费你80美元。(20或更少,如果你从eBay上购买你自己的USB接口的车载诊断)。

The aforementioned extern auto my_car also requires a diagnostic, and for that reason it is never run through the compiler, other than by city staff tasked with parking enforcement.

前面提到的外部自动my_car也需要诊断,因此它永远不会在编译器中运行,而不是由负责停车执行的城市工作人员运行。

If you see a lot of extern static auto ... in any code base, you're in a bad neighborhood; look for a better job immediately, before the whole place turns to Rust.

如果你看到很多外部的静态汽车…在任何代码库中,你都处于一个糟糕的社区;马上找一份更好的工作,然后整个地方就会生锈。

#3


28  

The auto keyword is useless in the C language. It is there because before the C language there existed a B language in which that keyword was necessary for declaring local variables. (B was developed into NB, which became C).

auto关键字在C语言中是没有用的。因为在C语言之前存在一种B语言,在该语言中,关键字是用来声明局部变量的。(B被发展成NB,成为C)。

Here is the reference manual for B.

这是B的参考手册。

As you can see, the manual is rife with examples in which auto is used. This is so because there is no int keyword. Some kind of keyword is needed to say "this is a declaration of a variable", and that keyword also indicates whether it is a local or external (auto versus extrn). If you do not use one or the other, you have a syntax error. That is to say, x, y; is not a declaration by itself, but auto x, y; is.

如您所见,手册中充满了使用auto的例子。这是因为没有int关键字。需要某种关键字来表示“这是一个变量的声明”,这个关键字还指示它是本地的还是外部的(自动对外)。如果您不使用其中一个或另一个,则会出现语法错误。也就是说,x y;不是声明本身,而是自动x, y;是多少。

Since code bases written in B had to be ported to NB and to C as the language was developed, the newer versions of the language carried some baggage for improved backward compatibility that translated to less work. In the case of auto, the programmers did not have to hunt down every occurrence of auto and remove it.

由于B中编写的代码库必须被移植到NB和C语言中,所以新版本的语言携带了一些改进后的兼容性,从而减少了工作。在自动的情况下,程序员不必搜索每一个发生的汽车并删除它。

It's obvious from the manual that the now obsolescent "implicit int" cruft in C (being able to write main() { ... } without any int in front) also comes from B. That's another backward compatibility feature to support B code. Functions do not have a return type specified in B because there are no types. Everything is a word, like in many assembly languages.

从手册中可以明显看出,在C语言中,现在过时的“隐式int”cruft(能够写入main(){…也来自B。这是支持B代码的另一个向后兼容特性。函数没有在B中指定的返回类型,因为没有类型。一切都是一个词,就像许多汇编语言一样。

Note how a function can just be declared extrn putchar and then the only thing that makes it a function that identifier's use: it is used in a function call expression like putchar(x), and that's what tells the compiler to treat that typeless word as a function pointer.

注意,一个函数是如何被声明的,然后是唯一的使它成为标识符使用的函数的东西:它在一个函数调用表达式中使用,比如putchar(x),这就是告诉编译器把那个没有类型的单词当作函数指针的方法。

#4


23  

In C auto is a keyword that indicates a variable is local to a block. Since that's the default for block-scoped variables, it's unnecessary and very rarely used (I don't think I've ever seen it use outside of examples in texts that discuss the keyword). I'd be interested if someone could point out a case where the use of auto was required to get a correct parse or behavior.

在C auto是一个关键字,表示一个变量是本地的一个块。因为这是块作用域变量的默认值,所以它是不必要的,而且很少使用(我认为我从来没有见过它在讨论关键字的文本示例之外使用它)。我感兴趣的是,如果有人能指出一种情况,那就是需要使用auto来得到正确的解析或行为。

However, in the C++11 standard the auto keyword has been 'hijacked' to support type inference, where the type of a variable can be taken from the type of its initializer:

但是,在c++ 11标准中,auto关键字被“劫持”来支持类型推断,其中变量的类型可以从其初始化器的类型中提取:

auto someVariable = 1.5;   // someVariable will have type double

Type inference is being added mainly to support declaring variables in templates or returned from template functions where types based on a template parameter (or deduced by the compiler when a template is instantiated) can often be quite painful to declare manually.

类型推断主要是添加到模板中声明变量,或者从模板函数返回,根据模板参数(或者在实例化模板时由编译器导出),手动声明通常是非常痛苦的。

#5


6  

With the old Aztec C compiler, it was possible to turn all automatic variables to static variables (for increased addressing speed) using a command-line switch.

使用旧的Aztec C编译器,可以使用命令行开关将所有自动变量转换为静态变量(以提高寻址速度)。

But variables explicitly declared with auto were left as-is in that case. (A must for recursive functions which would otherwise not work properly!)

但是,使用auto显式声明的变量是按原样保留的。(必须是递归函数,否则将无法正常工作!)

#6


4  

The auto keyword is similar to the inclusion of semicolons in Python, it was required by a previous language (B) but developers realized it was redundant because most things were auto.

auto关键字类似于在Python中包含分号,这是以前的语言(B)所要求的,但是开发人员意识到它是多余的,因为大多数东西都是自动的。

I suspect it was left in to help with the transition from B to C. In short, one use is for B language compatibility.

我怀疑它是为了帮助从B到c的过渡。简而言之,一个用途是B语言的兼容性。

For example in B and 80s C:

例如在B和80年代C:

/* The following function will print a non-negative number, n, to
   the base b, where 2<=b<=10.  This routine uses the fact that
   in the ASCII character set, the digits 0 to 9 have sequential
   code values.  */

printn(n, b) {
        extrn putchar;
        auto a;

        if (a = n / b)        /* assignment, not test for equality */
                printn(a, b); /* recursive */
        putchar(n % b + '0');
}

#7


0  

Auto keyword is a storage class (some sort of techniques that decides lifetime of variable and storage place) example. It has a behavior by which variable made by the Help of that keyword have lifespan (lifetime ) reside only within the curly braces

Auto关键字是一个存储类(一些决定变量和存储空间的生命周期的技术)。它有一个行为,由该关键字的帮助产生的变量的使用寿命(生命周期)仅位于花括号内。

{
    auto int x=8;        
    printf("%d",x);  // here x is 8

    { 
        auto int x=3;
        printf("%d",x);  // here x is 3
    }              

    printf("%d",x);  // here x is 8
}          

#8


-2  

auto is good for saying that you do not want it to be register. However it is often useless since the compiler will put register when neither been used and it thinks it is optimal, and it will seldom think so incorrectly, it will however often miss that register is optimal.

auto很好地表示您不希望它被注册。然而,它通常是无用的,因为编译器在未使用和它认为它是最优的时候,它认为它是最优的,而且它很少认为是错误的,它将会经常错过那个寄存器是最优的。

#1


64  

auto is a modifier like static. It defines the storage class of a variable. However, since the default for local variables is auto, you don't normally need to manually specify it.

auto是一个静态的修饰语。它定义了一个变量的存储类。但是,由于本地变量的默认值是auto,所以通常不需要手动指定它。

This page lists different storage classes in C.

此页面列出了C中的不同存储类。

#2


97  

If you'd read the IAQ (Infrequently Asked Questions) list, you'd know that auto is useful primarily to define or declare a vehicle:

如果你阅读了IAQ(不常见的问题)列表,你就会知道auto主要用于定义或声明车辆:

auto my_car;

A vehicle that's consistently parked outdoors:

一辆一直停在户外的车:

extern auto my_car;

For those who lack any sense of humor and want "just the facts Ma'am": the short answer is that there's never any reason to use auto at all. The only time you're allowed to use auto is with a variable that already has auto storage class, so you're just specifying something that would happen anyway. Attempting to use auto on any variable that doesn't have the auto storage class already will result in the compiler rejecting your code. I suppose if you want to get technical, your implementation doesn't have to be a compiler (but it is) and it can theoretically continue to compile the code after issuing a diagnostic (but it won't).

对于那些缺乏幽默感并且想要“仅仅是事实”的人来说,简单的回答是根本没有理由使用汽车。允许使用auto的唯一时间是有一个已经有自动存储类的变量,所以您只是指定了会发生的事情。试图在没有自动存储类的任何变量上使用auto将导致编译器拒绝您的代码。我想,如果您希望获得技术,您的实现不必是一个编译器(但它是),而且理论上它可以在发布诊断(但它不会)之后继续编译代码。

Small addendum by kaz:

小由kaz附录:

There is also:

还有:

static auto my_car;

which requires a diagnostic according to ISO C. This is correct, because it declares that the car is broken down. The diagnostic is free of charge, but turning off the dashboard light will cost you eighty dollars. (Twenty or less, if you purchase your own USB dongle for on-board diagnostics from eBay).

这需要根据ISO来诊断,这是正确的,因为它声明车坏了。诊断是免费的,但是关闭仪表板灯将花费你80美元。(20或更少,如果你从eBay上购买你自己的USB接口的车载诊断)。

The aforementioned extern auto my_car also requires a diagnostic, and for that reason it is never run through the compiler, other than by city staff tasked with parking enforcement.

前面提到的外部自动my_car也需要诊断,因此它永远不会在编译器中运行,而不是由负责停车执行的城市工作人员运行。

If you see a lot of extern static auto ... in any code base, you're in a bad neighborhood; look for a better job immediately, before the whole place turns to Rust.

如果你看到很多外部的静态汽车…在任何代码库中,你都处于一个糟糕的社区;马上找一份更好的工作,然后整个地方就会生锈。

#3


28  

The auto keyword is useless in the C language. It is there because before the C language there existed a B language in which that keyword was necessary for declaring local variables. (B was developed into NB, which became C).

auto关键字在C语言中是没有用的。因为在C语言之前存在一种B语言,在该语言中,关键字是用来声明局部变量的。(B被发展成NB,成为C)。

Here is the reference manual for B.

这是B的参考手册。

As you can see, the manual is rife with examples in which auto is used. This is so because there is no int keyword. Some kind of keyword is needed to say "this is a declaration of a variable", and that keyword also indicates whether it is a local or external (auto versus extrn). If you do not use one or the other, you have a syntax error. That is to say, x, y; is not a declaration by itself, but auto x, y; is.

如您所见,手册中充满了使用auto的例子。这是因为没有int关键字。需要某种关键字来表示“这是一个变量的声明”,这个关键字还指示它是本地的还是外部的(自动对外)。如果您不使用其中一个或另一个,则会出现语法错误。也就是说,x y;不是声明本身,而是自动x, y;是多少。

Since code bases written in B had to be ported to NB and to C as the language was developed, the newer versions of the language carried some baggage for improved backward compatibility that translated to less work. In the case of auto, the programmers did not have to hunt down every occurrence of auto and remove it.

由于B中编写的代码库必须被移植到NB和C语言中,所以新版本的语言携带了一些改进后的兼容性,从而减少了工作。在自动的情况下,程序员不必搜索每一个发生的汽车并删除它。

It's obvious from the manual that the now obsolescent "implicit int" cruft in C (being able to write main() { ... } without any int in front) also comes from B. That's another backward compatibility feature to support B code. Functions do not have a return type specified in B because there are no types. Everything is a word, like in many assembly languages.

从手册中可以明显看出,在C语言中,现在过时的“隐式int”cruft(能够写入main(){…也来自B。这是支持B代码的另一个向后兼容特性。函数没有在B中指定的返回类型,因为没有类型。一切都是一个词,就像许多汇编语言一样。

Note how a function can just be declared extrn putchar and then the only thing that makes it a function that identifier's use: it is used in a function call expression like putchar(x), and that's what tells the compiler to treat that typeless word as a function pointer.

注意,一个函数是如何被声明的,然后是唯一的使它成为标识符使用的函数的东西:它在一个函数调用表达式中使用,比如putchar(x),这就是告诉编译器把那个没有类型的单词当作函数指针的方法。

#4


23  

In C auto is a keyword that indicates a variable is local to a block. Since that's the default for block-scoped variables, it's unnecessary and very rarely used (I don't think I've ever seen it use outside of examples in texts that discuss the keyword). I'd be interested if someone could point out a case where the use of auto was required to get a correct parse or behavior.

在C auto是一个关键字,表示一个变量是本地的一个块。因为这是块作用域变量的默认值,所以它是不必要的,而且很少使用(我认为我从来没有见过它在讨论关键字的文本示例之外使用它)。我感兴趣的是,如果有人能指出一种情况,那就是需要使用auto来得到正确的解析或行为。

However, in the C++11 standard the auto keyword has been 'hijacked' to support type inference, where the type of a variable can be taken from the type of its initializer:

但是,在c++ 11标准中,auto关键字被“劫持”来支持类型推断,其中变量的类型可以从其初始化器的类型中提取:

auto someVariable = 1.5;   // someVariable will have type double

Type inference is being added mainly to support declaring variables in templates or returned from template functions where types based on a template parameter (or deduced by the compiler when a template is instantiated) can often be quite painful to declare manually.

类型推断主要是添加到模板中声明变量,或者从模板函数返回,根据模板参数(或者在实例化模板时由编译器导出),手动声明通常是非常痛苦的。

#5


6  

With the old Aztec C compiler, it was possible to turn all automatic variables to static variables (for increased addressing speed) using a command-line switch.

使用旧的Aztec C编译器,可以使用命令行开关将所有自动变量转换为静态变量(以提高寻址速度)。

But variables explicitly declared with auto were left as-is in that case. (A must for recursive functions which would otherwise not work properly!)

但是,使用auto显式声明的变量是按原样保留的。(必须是递归函数,否则将无法正常工作!)

#6


4  

The auto keyword is similar to the inclusion of semicolons in Python, it was required by a previous language (B) but developers realized it was redundant because most things were auto.

auto关键字类似于在Python中包含分号,这是以前的语言(B)所要求的,但是开发人员意识到它是多余的,因为大多数东西都是自动的。

I suspect it was left in to help with the transition from B to C. In short, one use is for B language compatibility.

我怀疑它是为了帮助从B到c的过渡。简而言之,一个用途是B语言的兼容性。

For example in B and 80s C:

例如在B和80年代C:

/* The following function will print a non-negative number, n, to
   the base b, where 2<=b<=10.  This routine uses the fact that
   in the ASCII character set, the digits 0 to 9 have sequential
   code values.  */

printn(n, b) {
        extrn putchar;
        auto a;

        if (a = n / b)        /* assignment, not test for equality */
                printn(a, b); /* recursive */
        putchar(n % b + '0');
}

#7


0  

Auto keyword is a storage class (some sort of techniques that decides lifetime of variable and storage place) example. It has a behavior by which variable made by the Help of that keyword have lifespan (lifetime ) reside only within the curly braces

Auto关键字是一个存储类(一些决定变量和存储空间的生命周期的技术)。它有一个行为,由该关键字的帮助产生的变量的使用寿命(生命周期)仅位于花括号内。

{
    auto int x=8;        
    printf("%d",x);  // here x is 8

    { 
        auto int x=3;
        printf("%d",x);  // here x is 3
    }              

    printf("%d",x);  // here x is 8
}          

#8


-2  

auto is good for saying that you do not want it to be register. However it is often useless since the compiler will put register when neither been used and it thinks it is optimal, and it will seldom think so incorrectly, it will however often miss that register is optimal.

auto很好地表示您不希望它被注册。然而,它通常是无用的,因为编译器在未使用和它认为它是最优的时候,它认为它是最优的,而且它很少认为是错误的,它将会经常错过那个寄存器是最优的。