|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
The extern keyword
Hey,
I was hoping someone could clarify this for me - all the information about extern that I read seems ambiguous to me - I'm just not understanding it. From my limited understanding, the extern keyword would come in useful in this scenario: Code:
header.h <empty> file1.c #include "header.h" int i = 5; file2.c #include "header.h" extern int i; // print i to be 5 First question - is the header even necessary here, or could file2.c know about file1.c without them sharing a header? Secondly, would this be correct use of extern: Code:
header.h int i = 5; file1.c #include "header.h" extern int i; // print i to be 5 file2.c #include "header.h" extern int i; // print i to be 5 In this case, can the extern declaration be omitted in the .c files - can they simply print i without declaring it? Thanks for any help. |
|
#2
|
||||
|
||||
|
The first example is the intended usage. You don't need the header file for anything there. Remember that headers are simply pasted in by the preprocessor when you #include them.
In your second example, because of this method of inclusion, both .c files will contain 'int i = 5' at the top, and you will get a linker error along the lines of 'multiply defined symbol 'i''. |
|
#3
|
|||
|
|||
|
Quote:
Ah OK I think I understand. So using the first example, any global variables in my program must have a unique name - in case of a situation like this? Code:
file1.c int i = 5; file2.c int i = 6; file3.c extern int i; // print i to be 5 or 6? Thanks. |
|
#4
|
|||
|
|||
|
In that case, you get the above mentioned error again.
Anything that is preceded by the extern keyword can only be declared in a global context once. You can use the same name as local variable inside a function, but like any other variable - only one declaration per context is allowed. That's why I've declared it in file1.c(only) below. Here's an example of how it may be of use: ---- file1.h ------ Code:
#ifndef file1_h #define file1_h extern int i; #endif --- file 1.c ------ Code:
int i; --- file2.h ------ Code:
#ifndef file2_h #define file2_h void demoFunc1(); #endif Code:
#include "file1.h"
void demoFunc1()
{
printf("See - I can access i. It is: %d\n", i);
}
--- file3.h ------ Code:
#ifndef file3_h #define file3_h void demoFunc2(); #endif -------- file3.c ------ Code:
#include "file1.h"
void demoFunc2()
{
printf("See - demofunc1 isn't the only one that can access i. It is: %d\n", i);
}
---- main.c ------- Code:
#include "file2.h"
#include "file3.h"
int main()
{
demoFunc1();
demoFunc2();
}
But do note - you won't be able to access this i from main.c, unless you include file1.h in main.c While the variable exists, the pre-processor knows nothing about it and will spit the dummy, refusing to compile main.c into main.o (main.obj). Long story short - wherever you want access to i, include "file1.h" |
|
#5
|
||||
|
||||
|
Quote:
Either way globals are just a bad idea and easily avoided. See this for example: A pox on globals . It relates to embedded systems, but the arguments are even more compelling in large systems, where resource constraint is no defence! Clifford. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > The extern keyword |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|