Using the extern keyword.

I’m trying to make a variable in the user_routines_fast.c file that can be accessed in the user_routines.c file. How do I do this. I think it uses the extern keyword? Am I wrong? If I’m not. How do I properly use it. Thanks.

Yes the you can use the keyword extern like the following:

//user_routines_fast.c
int foo;
...


//user_routines.c
extern int foo;

void test()
{
foo=2;
}

But the more professional way of doing this is by declaring the variable in user_routines.h where than you don’t have to use extern but rather only have to include the file user_routines.h (as it is already done in the default code)

no, do not define the variable in user_routines.h - this does not actually share the variable. instead, you get two copies, one in each file. (remember, putting something in an include file is just like inserting that text into each file in which you #include it). if you look at the user_routines.h file, you will notice that there is not a single actual variable defined there - there’s a reason for that.

extern is probably the way to go here, just declare the variable in one file, then copy the definition to the other but tack "extern " on the front. if you’re passing more than one thing back and forth, you might consider creating a struct to keep all of the info in one single variable that can be extern-ed (having more than one or two global variables looks messy :))

if you wanted to be ambitious, you could modify main.c so that Process_Data_From_Local_IO() and Process_Data_From_Master_uP() could return and be called with a value, thus passing the info that way (using a struct)

Hmm, when the variable is changed in user_routines.c, do you have to extern it back in u_r_f.c? Like so:

/*user_routines_fast.c*/
int foo = 263;
extern user_routines_function();    /*Is this extern needed?*/
extern foo = foo - 1;               /*Do we need the extern now,
                                     *because it was modified in u_r.c?*/

/*user_routines.c*/
user_routines_function()
{
  extern foo = foo + 3;
}

:slight_smile:

extern should never be used inside an expression like that. It is only used in declarations made at the file-scope level. Thus, you just do stuff like this:

/user_routines_fast.c/
int foo;

void myfunc(){
foo=foo+1;
}

/user_routines.c/
extern int foo;

void myfunc2(){
foo=foo+3;
}

They keyword extern is NEVER needed with function prototypes, as they are extern implicitly.

-Rob

extern is only used in the first declaration of variable in a file - treat it just as if you were declaring the variable normally, extern just says “this variable was originally declared in another file, and the same copy should be used here”.

also, remember that exactly one file should have the variable declared without “extern” - all of the other files extern this original declaration.

Finally, remember that extern only really works for global variables. So make sure your extern-ed variables are declared outside of any function calls.

–edit-- lol, it happened again.

I hate globals, and I hate the extern keyword.

If you can pass the variable as a parameter to any function where it’s needed, do it that way. It’s much cleaner, and less error-prone.

only argument i’d have against that is in this case, that would mean modifying the main.c loop and code, which a beginner programmer might not want to do (however, I agree for the most part and would/will probably implement it in such a way myself).