如何在C中引用main函数之后的静态全局变量

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    // write code here
    printf("%dn", a);
    
    return EXIT_SUCCESS;
}

static int a = 10;

如何a在注释位置引用静态全局变量?

我知道amain 之前的那个地方不会导致这个问题。我只想知道是否有任何相关的语法可以做到这一点?

回答

您可以像这样为before添加一个暂定定义amain

#include <stdio.h>
#include <stdlib.h>

static int a;    // tentative definition

int main(int argc, char *argv[])
{
    // write code here
    printf("%dn", a);
    
    return EXIT_SUCCESS;
}

static int a = 10;   // "full" definition

虽然如果你这样做,你也可以把完整的定义移到那个点。


以上是如何在C中引用main函数之后的静态全局变量的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>