Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?

int x = 5;
    void main()
    {
        int x = 3;
        m();
        printf("%d", x);
    }
    void m()
    {
        x = 8;
        n();
    }
    void n()
    {
        printf("%d", x);
    }

 

  • A.8   3
  • B.8   5
  • C.3   8
  • D.5   3

Answer: A

Correct answer: 8 3

main declares a local x = 3, which shadows the file-scope x = 5 inside main only. m is outside that scope, so its x = 8 assigns to the global, and n prints that 8. Back in main, the local is still 3.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.