简体   繁体   English

此代码在C语言中的输出是什么?

[英]what is the output of this code in C?

I found this code in page no. 我在页码中找到了此代码。 127 of this Book writer says it print 42. But when i try this it prints some garbage value. 这本书的作者127说它打印42。但是当我尝试这样做时,它打印一些垃圾值。 Why this is so? 为什么会这样呢?

    #include <stdio.h>
    void foo()
    {
        int a ;
        printf("%d \n", a);
    }

    void bar()
    {
        int a = 42;
    }

    int main()
    {
        bar();
        foo();
        return 0;
    }

a in foo() is uninitialized, so it's undefined behavior. afoo()未初始化,所以这是不确定的行为。

However, in practice, some compilers actually do output 42 (especially if optimization is off). 但是,实际上,某些编译器实际上会输出42 (尤其是在优化关闭的情况下)。 That's because after the call of bar() , the value 42 is left in the stack. 这是因为在调用bar() ,值42留在了堆栈中。 And inside foo() , the uninitialized a gets it. foo()内部,未初始化的a会得到它。 Again, it's undefined behavior so anything may happen, just don't do it. 同样,这是未定义的行为,因此任何事情都可能发生,只是不要这样做。

In foo() the variable a is uninitialized. foo() ,变量a未初始化。 Printing such a variable is garbage. 打印这样的变量是垃圾。 The function bar() has no meaning at all - and is likely removed from the optimizer during compile time. 函数bar()完全没有意义-可能会在编译时从优化器中删除。

a in function foo is a local variable of this function and in function bar is also a local variable and it has nothing to do with function bar's a in函数foo是此函数的局部变量,in函数条也是局部变量,与函数条的a无关

so when you assign a value to a in function bar there is no changes in a of function foo since then you see garbage value when you print a in foo 因此,当您为in功能栏分配值时,foo函数的a不会发生任何变化,因为当您打印in in foo时会看到垃圾值

The value of a defined in bar() is only in scope for the duration of that function. 在bar()中定义的值仅在该函数期间有效。 When a is defined again in foo() the compiler reassigns a and you have no guarantee what the memory stores. 当在foo()中再次定义a时,编译器将重新分配a,您将无法保证内存存储了什么。 You can't assume that it will overwrite the original a and point to 42. 您不能假定它将覆盖原始a并指向42。

The way to get the function to print 42 would be to define a in main(), then set a=42 in bar() and get rid of the redefinition in foo(). 获取函数以打印42的方法是在main()中定义a,然后在bar()中设置a = 42并摆脱foo()中的重新定义。

Are sure you haven't copied out a counter example? 确定您没有复制反例吗? Either that or you should treat your book with suspicion from now on. 从那以后,或者您应该怀疑自己的书。

The answer of Yu Hao explains it the best way, I want to modify your code such that it (works): Yu Hao的答案以最佳方式说明了这一点,我想修改您的代码以使其正常运行:

#include <stdio.h>
void foo(int a)
{
    printf("%d \n", a);
}

void bar(int *a)
{
    *a = 42;
}

int main()
{
    int b;
    bar(&b);
    foo(b);
    return 0;
}

The value of a is uninitialized in foo() function. a的值在foo()函数中未初始化。 if the heap stack in runtime memory is emty, then it will print garbage value, else print the first existed value. 如果运行时内存中的堆堆栈为emty,则它将打印垃圾值,否则将打印第一个存在的值。 You can thange the foo() to: 您可以将foo()转换为:

void foo()
{
    int d ;
    printf("%d \n", d);

    int e ;
    printf("%d \n", e);
}

the sencond line will print garbage value, such as 124992. So the result is relate to the heap stack in runtime memory. 第二行将打印垃圾值,例如124992。因此结果与运行时内存中的堆堆栈有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM