I think 'local extern' is a misnomer, local means just that, extern means it is defined in a different code module so it can't be local at the same time.
I´m no C expert, so please experts correct me if I´m wrong:
Just some notes:
"local" does not need to be written in the code (never), because it is the default - if a variable is declared in a block or function.
"global" does not need to be written either. A variable is called global if it is declared outside of all blocks/functions. (usually at the top of a file)
"external" needs to be written to a variable declaration inside a block/function ... Wrong: to notify that the variable/value should be available even outside the block/function. [Edited (greyed out) after post#5 to avoid spreading wrong informations. Please refer to post#5]
Almost Klaus - we will make a 'C' programmer of you yet :-D
You are right about the local and global definitions but 'extern' is used when more than one source file refers to the same variable. One source file defines it, the others refer to it as 'extern' so the linker knows they are the same thing and not a new variable with the same name.
example: file_1.c contains
int MyVariable;
file_2.c contains:
extern int MyVariable;
then 'MyVariable' is the same storage location for both files. The 'extern' reference can also be in a header (.h or .inc) file with the same effect. Without it, the second or subsequent files will generate their own storage with the name being local to that source file only.
There are external and public references used in C++ to specify which variables or functions are shared but that's a different matter.
Thanks Brian for clarifying this (I thought it was the other way round).
I´m still learning. Currently I can read C code quite good, but am unable to write "clean" C code.
Almost Klaus - we will make a 'C' programmer of you yet :-D
You are right about the local and global definitions but 'extern' is used when more than one source file refers to the same variable. One source file defines it, the others refer to it as 'extern' so the linker knows they are the same thing and not a new variable with the same name.
example: file_1.c contains
int MyVariable;
file_2.c contains:
extern int MyVariable;
then 'MyVariable' is the same storage location for both files. The 'extern' reference can also be in a header (.h or .inc) file with the same effect. Without it, the second or subsequent files will generate their own storage with the name being local to that source file only.
There are external and public references used in C++ to specify which variables or functions are shared but that's a different matter.
isn't really valid code although it might compile.
You wouldn't normally place it in a function, it would go at the top of the source file as "extern int MyVariable;" to indicate it was defined in a different source file.
In a program with a single source (.c) file 'extern' has no meaning. It is used when there is more than one source file, the definition in one and the 'extern' reference in the others. When a program has more than one source file, each source is compiled as though it was a program in its own right. Before it can be come an executable program, all the source files and any libraries used are fed to a 'linker' program that resolves the cross references between them so they can share variables and functions if necessary. The 'extern' tells the linker not to allocate space to store the variable but instead to use the space already allocated in another source file. Hence, each source file can 'see' the same variable even though it wasn't defined within its own code.
So in your case it would be something like:
Source file 1:
Code:
int MyVariable = 100;
and in source file 2:
Code:
extern int MyVariable;
int main()
{
MyVariable++;
printf( "MyVariable; = %d", MyVariable);
return 0;
}
@betwixt I have very difficult time to understand storage class in c language. I have spend so much time on google and tried to understand basic
I am describing life time and scope of variable Take a look at code
Code:
#include <stdio.h>
int a = 1;
static int b = 1;
extern int c = 1;
void storage ()
{
int x = 1;
static int y = 1;
x++;
y++;
printf(" x = %d\n", x) ;
printf(" y = %d\n", y) ;
}
int main ()
{
printf(" a = %d\n", a) ;
printf(" b = %d\n", b);
printf(" c = %d\n", c);
c++;
printf(" c = %d\n", c);
storage ();
printf("\n");
storage ();
return 0;
}
a = 1 ( a is accessible in this source file and it will stay alive for life time of program )
b = 1 ( b is accessible in this source file and it will stay alive for life time of program )
c = 1 ( c is accessible in this source file as well as other source file and it will stay alive for life time of program )
c = 2
x = 2 ( x is accessible in function storage only and it will stay alive until function exist )
y = 2 ( y is accessible in function storage only and it will stay alive for life time of program )
x = 2
y = 3
Remember : you explained use of extern keyword with different source and I am agree with you but I am using extern in same soure in my example and it's valid
a is accesible in this source file, and in other source files if they have an "extern" declaration
b is only accessible in this source file
c is declared somewhere else (that is the meaning of "extern"), and the assignment in the extern declaration is meaningless, even if it compiles.
Edit:
I now see that you have compiled and executed the source code. It seems that the compiler/linker is too kind to you. I expected a linking error if you don't have a declaration of c without "extern" somewhere.
- - - Updated - - -
My compiler does not like your code (extern declaration with assignment), but it compiles:
Code:
linux% make ext
cc ext.c -o ext
ext.c:5:15: warning: 'c' initialized and declared 'extern' [enabled by default]
extern int c = 1;