Answer SL30


x in main: 999
x in foo: 10
x in foo: 20
x in bar: 20
x in bar: 40
x in main: 999

/* --- fileA.c --- */
int x = 8;    /* external linkage, file scope */

 
/* --- fileB.c --- */
#include <stdio.h>
 
static int x = 10 ;  /* internal linkage, file scope */

void foo( )
{
  printf("x in foo: %d\n", x );
  x = x+10 ;
  printf("x in foo: %d\n", x );
}

void bar( )
{
  printf("x in bar: %d\n", x );
  x = 2*x ;
  printf("x in bar: %d\n", x );
}

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

static int x = 999; /* internal linkage, file scope */

void main()
{
  printf("x in main: %d\n", x );
  foo();
  bar();
  printf("x in main: %d\n", x );
}



Comments: main() sees the x in its file that has internal linkage. The two functions in fileB.c both see the x in that file that has internal linkage (which is a different x from what main() sees. There is an external object, also named x, but no function uses it.



Back to Puzzle Home