小编Zee*_*shi的帖子

为什么静态全局变量可以在其他文件中访问?

//a.c
#include <stdio.h>
#include "b.c"
void main()
{
    int var;
    var = increment();
    var = increment();
    var = increment();
    count = count + 3;
    var = count;
    printf("%d", var);
}
Run Code Online (Sandbox Code Playgroud)
//b.c
static int count;
int increment()
{
    ++count;
    return count;
}
Run Code Online (Sandbox Code Playgroud)

现在就像在 bc 文件中一样,我有一个名为 count 的变量并且是静态的。现在这个变量不应该直接在 ac 中访问,但在我的情况下我可以访问和操作它。所以我错过了什么吗?

OUTPUT
6
Run Code Online (Sandbox Code Playgroud)

c variables static scope global-variables

3
推荐指数
1
解决办法
65
查看次数

标签 统计

c ×1

global-variables ×1

scope ×1

static ×1

variables ×1