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;
        printf("%d", x);
        {
            x = 4;
        }
        printf("%d", x);
    }

 

  • A.Run time error
  • B.3  3
  • C.3  5
  • D.3  4

Answer: D

Correct answer: 3 4

The inner x = 4; is an assignment, not a declaration, so it changes the x already in scope - main's local. The first printf shows 3 and the second, after the block, shows 4. The file-scope x = 5 is shadowed throughout and never seen.

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

Post your comment

Please log in or register to post a comment.