Does the function have a storage class in C?

Does any function have a storage class in C?

+5
source share
2 answers

The answer is no. According to http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (draft C99) and http://www.open-std.org/jtc1/sc22/wg14/www /docs/n1570.pdf (draft C11):

6.2.4 Duration of storage of objects

1 The object has a storage duration that determines its lifetime.

Functions are not objects, so they do not have storage.

6.2.2 Identifier associations

3 If the declaration of the file area identifier for an object or function contains a static storage class specifier, the identifier has an internal binding.

This suggests that static applied to the function affects its binding (there is no storage to which it can relate).

+3
source

Standard C does not formally define the value of a storage class.

It defines what a “storage class specifier” is — one of the keywords typedef , extern , static , _Thread_local , auto and register .

Functions can be declared using storage class specifiers extern or static .

The standard refers to objects that have a "storage class" in several places, for example

If the array object has a register storage class, the behavior is undefined

but it’s never determined what an object storage class is. It might be reasonable to assume that this is a storage class specifier keyword that appears in one of the declarations, but it remains unclear what will happen if some declarations of the same object have a storage class specifier and others do not. It is also not defined what is the storage class of an object that does not have a declaration with a storage class specifier.

It seems that you should avoid talking about the storage classes of objects or functions in general, and instead use the related concepts of storage duration and binding, which are precisely defined by the standard. When necessary, use phrases such as “storage class specifier X” in the declaration, but not “object / function has storage class X”.

+3
source

All Articles