1

Global variables have scope limited to the particular file in which they are defined.By the use of Extern we can increase their visibility to other files.Why there is no error due to missing definition of func() or without using extern in line-1?

Even if that is allowed, in the following program, global variable 'x' of File1 and File2 seems to have same scope all over the two files even without using extern?

FILE1.c

#include <stdio.h>
void func(void);   // line-1
int x = 15213;     // line-2
int main()
{
    func();
    printf("x = %d\n", x);
    return 0;
}

FILE2.c

int x;
void func()
{
    x = 15212;
}

Output of the above program is 15212

How's this output is coming even when extern is not used in line-2? Help is appreciated!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Freemn
  • 23
  • 7

0 Answers0