How can I use a variable in one .c
file when it has been previously defined in another .c
file?
-
12I changed your title. Try to make the title more descriptive, so that 1) people who're able to answer it can see easily what you're asking, and 2) so others who have the same question, will be able to find this so they don't have to ask it again. When looking at the list of questions, we don't need to know that it's a beginner question, but we do need to know what the question is about. Just a tip for future questions. :)Stack Overflow is garbage– Stack Overflow is garbage2009年06月25日 18:51:13 +00:00Commented Jun 25, 2009 at 18:51
6 Answers 6
In fileA.c:
int myGlobal = 0;
In fileA.h
extern int myGlobal;
In fileB.c:
#include "fileA.h"
myGlobal = 1;
So this is how it works:
- the variable lives in fileA.c
- fileA.h tells the world that it exists, and what its type is (
int
) - fileB.c includes fileA.h so that the compiler knows about myGlobal before fileB.c tries to use it.
9 Comments
extern int myGlobal;
directly into the source file that wants to access myGlobal
, but that's a nasty violation of DRY). If you need to share a global from main.c, create main.h!In 99.9% of all cases it is bad program design to share non-constant, global variables between files. There are very few cases when you actually need to do this: they are so rare that I cannot come up with any valid cases. Declarations of hardware registers perhaps.
In most of the cases, you should either use (possibly inlined) setter/getter functions ("public"), static variables at file scope ("private"), or incomplete type implementations ("private") instead.
In those few rare cases when you need to share a variable between files, do like this:
// file.h
extern int my_var;
// file.c
#include "file.h"
int my_var = something;
// main.c
#include "file.h"
use(my_var);
Never put any form of variable definition in a h-file.
18 Comments
if the variable is :
int foo;
in the 2nd C file you declare:
extern int foo;
Comments
- Try to avoid globals. If you must use a global, see the other answers.
- Pass it as an argument to a function.
5 Comments
Those other variables would have to be declared public (use extern, public is for C++), and you would have to include that .c file. However, I recommend creating appropriate .h files to define all of your variables.
For example, for hello.c, you would have a hello.h, and hello.h would store your variable definitions. Then another .c file, such as world.c would have this piece of code at the top:
#include "hello.h"
That will allow world.c to use variables that are defined in hello.h
It's slightly more complicated than that though. You may use <> to include library files found on your OS's path. As a beginner I would stick all of your files in the same folder and use the " " syntax.
Comments
The 2nd file needs to know about the existance of your variable. To do this you declare the variable again but use the keyword extern
in front of it. This tells the compiler that the variable is available but declared somewhere else, thus prevent instanciating it (again, which would cause clashes when linking). While you can put the extern
declaration in the C file itself it's common style to have an accompanying header (i.e. .h
) file for each .c
file that provides functions or variables to others which hold the extern
declaration. This way you avoid copying the extern
declaration, especially if it's used in multiple other files. The same applies for functions, though you don't need the keyword extern
for them.
That way you would have at least three files: the source file that declares the variable, it's acompanying header that does the extern
declaration and the second source file that #include
s the header to gain access to the exported variable (or any other symbol exported in the header). Of course you need all source files (or the appropriate object files) when trying to link something like that, as the linker needs to resolve the symbol which is only possible if it actually exists in the files linked.