为什么gcc会给我这个结果?

Gau*_*aui 0 c gcc

当我运行此代码时,gcc给出了输出10.

有人可以向我解释为什么它会给我10个?:)

#include <stdio.h>

int f(int x) {
    int y;
    y = 2*x;
}

int g() {
    int z;
    return z;
}

int main() {
    int x=5;
    f(x);
    printf("%d\n",g());
}
Run Code Online (Sandbox Code Playgroud)

che*_*red 6

这是未定义的行为 - 您正在引用一个没有设置值的变量.可能,它给出了10,因为编译器在f()中使用了相同的内存位置,但是不能保证,它不应该依赖,并且只不过是好奇心.