I had read that if I want to use a global variable in another C file,I should precede the data type with the access specifier extern like that:
File One File Two
int x, y; extern int x, y;
char ch; extern char ch;
int main(void) void func22(void)
{ {
/* ... */ x = y / 10;
} }
void func1(void) void func23(void)
{ {
x = 123; y = 10;
} }
and by mistake in file two I don't declare x, y as they are. I declared them as int x
and int y
and I wait the compiler to display an error of redefinition of variables x and y, but the code runs without any errors.
for example:
File One
#include <stdio.h>
int global = 1000; /** declare global variable called
global **/
int main(){
print();
return 0;
}
File Two
int global; /** here I don't use the extern keyword **/
void print(void){
global = 100;
printf("\n global = %d",global);
return;
}
the result is : global = 100
The program is compiled and linked without any error
So my question is: Is extern
specifier is redundant here ?!