简体   繁体   English

Function 块内声明 C

[英]Function declaration inside of block in C

Does the C standard (C99 +) require the implementation to allow function declarations to be placed within a block in order to limit their scope to that block and where is this covered in the standard ? C 标准 (C99 +) 是否要求实现允许将 function 声明放置在一个块内,以便将其 scope 限制在该块内,标准中涵盖了哪些内容? Assume the function has external linkage and is defined in a separate source file to be included when linking.假设 function 具有外部链接,并且在链接时包含在单独的源文件中定义。

I noticed that GCC generates an error when the following program is compiled:我注意到编译以下程序时 GCC 会产生错误:

int main(void)
{
  void myFunc(void);

  myFunc();

  return 0;
}

void test2(void)
{
  myFunc();
}

Error (which is expected):错误(这是预期的):

..\main.c: In function 'test2':
..\main.c:12:3: warning: implicit declaration of function 'myFunc' [-Wimplicit-function-declaration]
   12 |   myFunc();
      |   ^~~~~~
..\main.c:3:8: note: previous declaration of 'myFunc' was here
    3 |   void myFunc(void);
      |        ^~~~~~
..\main.c:12:3: error: incompatible implicit declaration of function 'myFunc'
   12 |   myFunc();
      |   ^~~~~~
..\main.c:3:8: note: previous implicit declaration of 'myFunc' was here
    3 |   void myFunc(void);
      |        ^~~~~~

This would be expected since myFunc() is declared within the scope of main() .这是意料之中的,因为myFunc()是在main()的 scope 中声明的。

If the call to myFunc() is removed from test2() , and test2() is defined in another source file included when linking, then the program compiles and links with no errors or warnings.如果从test2()中删除了对myFunc()的调用,并且在链接时包含的另一个源文件中定义了test2() ,则程序编译和链接时不会出现错误或警告。

Based on this result, the answer to the question would be yes, but I am wondering if this behavior is explicitly defined in the specification and can be considered portable.基于这个结果,问题的答案是肯定的,但我想知道这种行为是否在规范中明确定义并且可以被认为是可移植的。

Within the function function内

void test2(void)
{
  myFunc();
}

the name myFunc is undeclared.名称myFunc未声明。

The declaration of the function is visible only within main. function 的声明仅在 main 中可见。

For backward compatibility the compiler considers that the function has the return type int .为了向后兼容,编译器认为 function 具有返回类型int

As for your question then you may declare a function within a block scope but without a storage class specifier except extern (the C Standard, 6.7.1 Storage-class specifiers):至于你的问题,那么你可以在块 scope 中声明一个 function 但没有存储 class 说明符,除了extern (C 标准,6.7.1 存储类说明符):

6 The declaration of an identifier for a function that has block scope shall have no explicit storage-class specifier other than extern. 6 具有块 scope 的 function 的标识符声明除 extern 外不应有明确的存储类说明符。

That is you may not declare a function in a block scope for example like也就是说,您不能在块 scope 中声明 function,例如

int main(void)
{
  static void myFunc(void);

  myFunc();

  return 0;
}

Does the C standard (C99 +) require the implementation to allow function declarations to be placed within a block in order to limit their scope to that block? C 标准 (C99 +) 是否要求实现允许将 function 声明放置在一个块内,以便将其 scope 限制在该块内?

Yes.是的。

where is this covered in the standard?这包含在标准中的什么地方?

Follow the grammar: function-definition -> compund-statement -> block-item-list -> block-item -> declaration-specifiers -> [ declaration-specifiers -> type-specifier void + init-declarator-list -> init-declarator -> declarator -> [ direct-declarator myFunc + ( + empty identifier-list + ) ]+ ;遵循语法:函数定义-> compund-statement -> block-item-list -> block-item -> declaration-specifiers -> [ declaration-specifiers -> type-specifier void + init-declarator-list -> init -declarator -> declarator -> [ direct-declarator myFunc + ( + empty identifier-list + ) ]+ ; ]. ].

The notation used to represent the grammar is also explained: https://port70.net/~nsz/c/c99/n1256.html#6.1p1 .用于表示语法的符号也有解释: https://port70.net/~nsz/c/c99/n1256.html#6.1p1

In other words, a function definition has a block of code, which consist of declarations and expressions.换句话说,一个 function 定义有一个代码,它由声明和表达式组成。 And a function declaration is one of possible declarations, so it can be inside the block inside a function definition. function 声明是可能的声明之一,因此它可以在 function 定义的块内。

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

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