c - 将变量声明放在 C 中的单独函数中

标签 c

是否可以将变量声明放在外部函数中?从 Wikipedia 阅读后那:

an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined.

我假设以下可能有效。没过多久,编译器就打了我的手指:(

inline void declaration(){
    int a;
}

int main(){
    declaration();
    a=2;
    return 0;
}

最佳答案

这可能不是它的完成方式,但如果您想了解如何考虑内联函数时会发生什么的基本概念。

想象一下编译器将你的代码变成这样的东西,然后你就会明白为什么它不起作用。

int main(){
    {
        int a;
    }
    a=2;
    return 0;
}

declaration() 的调用被包括括号的函数内容所取代,因此 int a; 是在内部范围内声明的,在内部范围内是不可见的主要功能。

关于c - 将变量声明放在 C 中的单独函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9613977/

相关文章:

c++ - 用于在链表中查找连接点的生产代码

c++ - 从 PostgreSQL DB 和 Linux 转移 C/C 中的时间戳?

c - 包含 gcc 的链接器选项

c++ - 在CentOS中编译C遇到错误: undefined reference to

c - 如何安全地将一组 C 函数实现为自初始化库?

c - 如何防止 get() 和 set() 之间的竞争条件行为?

c - 这就像函数调用参数是在 BNF 语法中定义的吗?

ios - 如何在 ios 中定义多个 c 标志?

C 中的 CHIP8 - 如何正确处理延迟定时器?

c - 使用哈希函数