是否可以将变量声明放在外部函数中?从 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/