什么是C局部函数声明机制? [英] What is C local function declaration mechanism?

查看:165
本文介绍了什么是C局部函数声明机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc中似乎允许使用局部函数声明,我对此进行了讨论:

Local function declaration seems to be permitted in gcc, and I found a discussion on this: Is there any use for local function declarations?

但是,我的问题是:ISO C标准允许吗?如果是这样,如何解释以下令人费解的现象:

However, my question is: is it allowed by ISO C standard? If it is, how to explain the following phenomenon which makes it puzzling:

int main(void) {
    int f(void);
    f();
}
void g(void) {
    /* g has no idea about f. It seems that the decl is limited within its
     * scope */
    f(); 
}
int f(void) {}

同时

int main(void) {
    int f(void);
    f();
}
void f(void); /* error because disagreement with the local declaration (local
             declaration goes beyound its scope?) */
void f(void) { /* definition here */ }

根据C99标准:函数名称在同一命名类别中。因此,我们将讨论范围界定机制以对此进行解释。但是如何?

According to the C99 standard: function names are in the same naming category. So we shall discuss the scoping mechanism to explain this. But how?

实际上,我正在从事一个编译器课程项目,该项目要求我们实现C编译器的简化版本。我试图处理这种情况,但感到困惑。

Actually, I'm working on a compiler course project which requires us to implement a simplified version of C compiler. I was trying to deal with this case but got confused.

编辑:我知道C是面向过程的并且需要函数名称要唯一。但是这种地方性的声明引起了明显的局面,我很难理解其原则/规则。

I know it's a common knowledge that C is procedure-oriented and requires function names to be unique. But this local style of declaration stirs clear situation, and it's hard for me to understand its principle/rule.

推荐答案

ISO C和C ++都允许局部函数声明。在每种情况下,函数声明的作用域都在本地作用域的末尾结束。但是,函数声明及其定义具有外部链接,因此仍然需要链接器接受。

Both ISO C and C++ permit local function declarations. In each case the scope of the function declaration ends at the end of the local scope. However, the function declaration and its definition have external linkage so they still need to be acceptable to the linker.

在您的示例中:

int main(void) {
 int f(void);
 f();
}
void f(void);
void f(void) {  }

此代码无错误地编译为C或C ++。在C中通常应该链接,但在C ++中则不应该链接(因为类型安全链接)。 int f(void);

This code compiles without error, as either C or C++. In C is should link (usually), but in C++ it will not (because of "typesafe linkage"). There will be an unresolved external for int f(void);.

会有一个未解决的外部问题。由于您的英语不便,请澄清一下,我将编辑答案。]

[If this does not answer your question because of difficulties with your English, please clarify and I'll edit the answer.]

C标准包含以下内容。 n1570 / S6.7.1 / 7:

The C standard contains the following. n1570/S6.7.1/7:


具有块范围的函数的标识符声明应没有显式的

The declaration of an identifier for a function that has block scope shall have no explicit storage-class specifier other than extern.

明确允许使用局部函数声明。

Clearly local function declarations are explicitly permitted.

n1570外部链接上的S6.2.2 / 5:

n1570 S6.2.2/5 on external linkage:


如果声明函数标识符没有存储级说明符,则其链接
的确定与使用存储级说明符extern声明时完全相同。如果
对象标识符的声明具有文件范围且没有存储类说明符,则
的链接是外部的。

If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

因此,局部函数声明具有外部链接。这很明显:如果他们有内部链接或没有链接,则它们将无法链接到任何东西。

So local function declarations have external linkage. This is obvious: if they had internal or no linkage, they could not link to anything.

这篇关于什么是C局部函数声明机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆