Answer SL26


x before foo: 444
x after  foo: 444

Comments: The linker creates and initializes external variables when it creates the executable file. There is just one external variable x in this project, which is initialized to 444 before execution begins.

When foo() executes, it changes its local variable x. But this x has no linkage and is a different x than the external one. It also has block scope, which hides the other x in fileB.c in that scope.


/* --- fileB.c --- */
int x = 444 ;    /* This x has file scope, external linkage */
void foo()
{
  int x;         /* This x has block scope, no linkage. */
  x = 99;        /* It is a different x than the external one. */
}

/* --- fileC.c --- */
#include <stdio.h>
void foo();

int x ;         /* This x has file scope, external linkage. */
                /* It is the same x as the one initialized to 444 */
void main()
{
  printf("x before foo: %d\n", x );
  foo();
  printf("x after  foo: %d\n", x );
}



Back to Puzzle Home