简体   繁体   English

函数是否在C语言中有任何存储类?

[英]Does a function have any storage class in C Language?

函数是否在C语言中有任何存储类?

The answer is no. 答案是不。 According to http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (draft of C99) and http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf (draft of C11): 根据http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99草案)和http://www.open-std.org/jtc1/sc22/wg14 /www/docs/n1570.pdf(C11草案):

6.2.4 Storage durations of objects 6.2.4对象的存储持续时间

1 An object has a storage duration that determines its lifetime. 1对象具有确定其生命周期的存储持续时间。

Functions aren't objects, so they have no storage. 函数不是对象,因此它们没有存储空间。

6.2.2 Linkages of identifiers 6.2.2标识符的链接

3 If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static , the identifier has internal linkage. 3如果对象或函数的文件范围标识符的声明包含存储类说明符static ,则标识符具有内部链接。

This says that static applied to a function affects its linkage (there is no storage it could apply to). 这表示应用于函数的static会影响其链接(没有可以应用的存储)。

The C standard doesn't formally define the meaning of storage class . C标准没有正式定义存储类的含义。

It does define what a "storage-class specifier" is --- it is one of the keywords typedef , extern , static , _Thread_local , auto and register . 它确实定义了什么是“存储类说明符”---它是关键字typedefexternstatic_Thread_localautoregister

Functions can be declared with storage-class specifiers extern or static . 可以使用存储类说明符externstatic声明函数。

The standard does mention objects having "storage class" in several places, for example 例如,该标准确实提到了在几个地方具有“存储类”的对象

If the array object has register storage class, the behavior is undefined 如果数组对象具有寄存器存储类,则行为未定义

but it is never defined what a storage class of an object is. 但永远不会定义对象的存储类是什么。 One could reasonably assume that this is the storage-class specifier keyword that appears in one of the declarations, but it remains unclear what happens if some declarations of the same object have a storage-class specifier and others don't. 可以合理地假设这是存在于其中一个声明中的存储类说明符关键字,但是如果同一对象的某些声明具有存储类说明符而其他声明没有,则仍然不清楚会发生什么。 It is also never defined what is the storage class of an object that doesn't have any declaration with a storage-class specifier. 它也从未被定义为没有任何带有存储类说明符的声明的对象的存储类。

It seems that one should avoid talking about storage classes of objects or functions altogether, and instead use related notions of storage duration and linkage which are precisely defined by the standard. 似乎应该避免完全讨论对象或函数的存储类,而是使用由标准精确定义的存储持续时间链接的相关概念。 When necessary, use phrases like "storage-class specifier X appears in a declaration", but not "object/function has storage class X". 必要时,使用“存储类说明符X出现在声明中”这样的短语,但不要使用“对象/函数具有存储类X”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM