简体   繁体   中英

static function declaration inside a block

A tutorial said

You cannot declare functions within a block as static.

What is the reason?How does it affect the function?

static when applied to functions means that the function has internal linkage . ie it can be accessed within this file only. In other words it has file-scope . This linkage applies irrespective of whether you declare a function inside another function or outside of all of them.

Now, if you apply the static keyword to a function declaration inside another block/function, semantically speaking , you are trying to restrict the scope of the function to that particular block/function, which goes against the language rules that state that static functions, irrespective of their place of declaration, shall have file-scope.

And hence, the standard might be prohibiting such a declaration.

PS : Footnote 30 in 6.2.3 in the latest C11 standard states-

A function declaration can contain the storage-class specifier static only if it is at file scope;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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