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

Discussion

Predict the output of the following C source code :

int x = 10;
int main( )
{
  int x = 20;
  {
    int x = 30;
    {extern x; printf("%d", x);}
  }
  return 0;
}

  • A.10
  • B.20
  • C.30
  • D.10 20 30

Answer: A

The program prints 10.

The innermost block declares extern x;, which (with the old implicit int) declares that x refers to an object with external linkage. That is the global int x = 10, not either local x, so within that block printf prints 10.

Without the extern declaration, the innermost local x = 30 would be printed. The value 20 belongs to the outer local that is hidden here, and only one printf runs, so '10 20 30' is impossible.

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

Post your comment

Please log in or register to post a comment.