Answer SL27


x before foo: 444
x as parameter: 7
x as parameter: 25
x after  foo: 444

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

When foo() executes, it changes its parameter 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 --- */
#include <stdio.h>

int x = 444 ;
void foo(int x)
{
  printf("x as parameter: %d\n", x );
  x = 25;
  printf("x as parameter: %d\n", x );
}

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

void main()
{
  extern int x ;
  printf("x before foo: %d\n", x );
  foo( 7 );
  printf("x after  foo: %d\n", x );
}



Back to Puzzle Home