小编SOF*_*ser的帖子

混淆静态int并在printf中调用它们

请测试此代码并给我你的答案:

#include <stdio.h>

int func() {
static int n = 0;
n++;
return n;
}

int main() {
    /*int first = func();
    int second = func();*/
    printf(" first call : %d \n second call : %d ",func(),func());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

从逻辑上讲,它应该打印1和2,但它打印2和1.如果取消注释注释并打印变量"first"和"second",问题就解决了!怎么了?

谢谢你!

c variables static

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

标签 统计

c ×1

static ×1

variables ×1