Extern variable.

file1.c

int a1;
int main()
{
        a1 = 2;
        printf("\na1 = %d\n", a1);
        next();
        printf("\na1 = %d\n", a1);
        next1();
        printf("\na1 = %d\n", a1);
}

file2.c

#include <stdio.h>

int b1 = 0;
void next(void)
{
        char a1;

        a1 = 'a';
        b1 = 77;
}

file3.c

int a1;
void next1(void)
{
        float b1;

        b1 = 20.3;
        a1 = 15;
}

When i compile it using gcc -c and create separate respective object file
-and try to link them it dosent give any errors as to "multiple declaration of variable" as of "variable a" in this case.

-And it gets comiled and linked successfully.
-also the "third printf" in file1.c prints the value of a=15 which is updated by file3.c.
-whereas its nowhere specified in file3.c that variable "a is an exterm variable"
in that case file1.c would printf the value of a1=15;
-so.................???????????????????????

Regards
Tanvir

Moved to High Level Programming Forum. FPMurphy

So, the gcc linker appears not to insist on the extern qualifier.
It looks in fact like the compler and linker is happy that a1 is declared and tacitly assumes the extern and the definition -- recall the difference in C between definition of a variable, which tells the compiler to allocate memory for it, and the declaration, which tells the compiler what type it is. The distinction is apparent in global variables, where

int foo = 3;

is both definition and declaration, but

extern int foo;

is just a declaration -- it says that a1 is of type int but memory for it is allocated elsewhere.

Note that

  1. If you define a1 in bothe file1.c and file2.c, e.g.
    text int a1 = 2;

    then the linker does complain
  2. If you compile bothe files in the same compilation unit, e.g.
    text gcc -o file12.o -c file1.c file2.c

    then the compiler complains.

The a1 variable in file2.c is protected by the lexical scope of the function next and so is completely different to the global int a1.