Solution for Exercise 24-2

ex2402 main.c Module

#include <stdio.h>
#include <stdlib.h>

void second(void);

int count;

int main()
{
    for(count=0;count<5;count++)
        second();
    return 0;
}

ex2402 second.c Module

#include <stdio.h>

extern int count;

void second(void)
{
    printf("%d\n",count+1);
}

Output

1
2
3
4
5

Notes

* The extern keyword merely identifies a variable expected to be elsewhere. It sets the variable's data type and name, which the compiler checks for proper usage in the code. It's the linker that hunts for the external variable in another module. If not found, a linker error pops up.