because a non static global variable inside a c file is only available inside the current file. So it makes not difference to use static or not...
It is invalid statement.
Compare two examples
main.c
1 2 3
int x = 10;
void f() { printf( "x = %d\n", x ); }
extra.c
1 2 3
int x = 20;
void g() { printf( "x = %d\n", x ); }
and
main.c
1 2 3
staticint x = 10;
void f() { printf( "x = %d\n", x ); }
extra.c
1 2 3
staticint x = 20;
void g() { printf( "x = %d\n", x ); }
In the first example the linker will issue an error or warning (depending on the linker) that the same name was defined twice. In the second example the code will be saccessfully compiled.
But i do not really understand why...
I always thought a global variable inside a c file ony was available for the functions inside that c file. So 2 globally defined variables with the same name in 2 different c files are 2 seperate things...(in my view)
when i define a global variable inside extra.c i can't access it from main.c and vice versa .. so why does the linker not make it behave like a static would?
when i define a global variable inside extra.c i can't access it from main.c and vice versa ..
You can. The same rules as with functions and anything else applies: you have to declare it before you can use it. In the case of a global variable: externint x;
You can. The same rules as with functions and anything else applies: you have to declare it before you can use it. In the case of a global variable: extern int x;
I know this... but in my example i don't use extern... so why do 2 non static variables inside 2 c files not work?
so why do 2 non static variables inside 2 c files not work?
It violates the one definition rule.
According to you, if you have two global variables x in two different translation units and a third one references x with externint x;, which one of the two is the linker supposed to pick?
The name of a global variable is kept in the table of external symbols which a linker uses to check the one definition rule. So if in two or more modules you have global variables with the same name then the linker issue an error if you do not specify that it is the same variable by using extern.
Opposite to global variables names of static variables do not stored in the table of extternal symbols so the linker knows nothing that they exist.
But i can understand when 2 variables with the same name in 2 c files without a extern are 2 seperate things... why is the linker not able to detect this?
Or is it only to prevent a future error when extern could be used?
Or is it only to prevent a future error when extern could be used?
More or less. A shared library loaded at runtime could have a reference to the variable, for example.
But it's just how it is. When you don't mark a variable or function as static, it has external linkage and therefore falls under the one definition rule. Chances are that if you have two variables or functions with the same name in different translation units, it's a mistake and you actually just want one. If you don't, you'd have declared them as static.