3

I have a variable which I have to share across multiple files. So, one way of accessing this variable in functions defined in other files is by passing it in arguments of function calls. Another way is using extern. Extern variables will work. But I am not sure what will be the major issues that I would have to face..

JessePinkman
  • 613
  • 8
  • 15
  • Pro: use what you like. Con: not thread safe – SwiftMango Jul 11 '19 at 05:07
  • 1
    Those extern variables are global variables. In general, one should avoid global variables. – Charlie Burns Jul 11 '19 at 05:29
  • Yeah, but why? @CharlieBurns – JessePinkman Jul 11 '19 at 06:16
  • 1
    Google "why are global variables bad". – hyde Jul 11 '19 at 06:57
  • Possible duplicate of [Are global variables bad?](https://stackoverflow.com/questions/484635/are-global-variables-bad) – KamilCuk Jul 11 '19 at 08:52
  • 3
    @CharlieBurns: In general one should avoid global variables, and various keywords (goto, inline, etc), and over half of the C library functions, and pointers, and heap, and functions with side-effects, and fine-grained locking, and anything that's implementation defined or undefined (including primitive types with implementation defined ranges), and macros, and third-party libraries, and the whole of C language (just to be sure), and all the other programming languages, and people who say things like "In general ... should be avoided". – Brendan Jul 11 '19 at 09:27

2 Answers2

6

One can share a variable between files in (at least) four ways. You already mentioned two of them:

  • using extern: it will be a global variable, anyone will be able to modify it, not thread-safe; the use of global variables should be minimized;
  • passing a pointer to the the variable: it will provide read / write access to the variable;
  • passing the value of the variable: provides read-only access to the variable;
  • using an access function.

The function can be as simple as:

float read_speed()
{
    return speed;
}

If the variable needs to be modified also, another function can be provided:

void write_speed(float new_speed)
{
    speed = new_speed;
}

The advantage of using access functions is that it separates the variable from the other files. If needed, you can provide several functions for the same variable, for different needs.

The cost is that code will increase a little and speed will decrease lightly.

Compare:

float read_speed_mph(); // get speed in miles per hour
float read_speed_mps(); // get speed in meters per second

They both return the same speed, expressed in different units. The body of the functions will make the right calculation to provide the correct value, while the variable will remain unmodified.

virolino
  • 2,073
  • 5
  • 21
0

In addition/as an concrete excample to what virolino wrote:

A combination of extern variable plus getter and setter functions ("access functions") is usually used for interface layers:

  • declare variable in header file as extern e.g:
    extern uint8_t var;

  • define variable in source file:
    uint8_t var= 5;

  • implement getter/setter in header e.g.:
    static inline uint8_t Get_Var(void) { return var; }

But as said before, you can directly manipulate var.

  • Why do you think that `var` cannot be manipulated directly? Actually, you just described the standard way to share a global variable. MAYBE you wanted to write nothing in the header, but make the variable `static` in the C file. – virolino Jul 11 '19 at 09:43
  • @virolino: I've tried to compile and I got a "multiple definition error". You don't get that error message? When you declare the variable as `static` in the C file, that collides with the `extern` declaration, right? I wanted to give a concrete excample of how to use external. – ElvisIsAlive Jul 11 '19 at 13:01
  • What is the exact error message? What do you compile? – virolino Jul 11 '19 at 13:06
  • I was wrong - you definitely can manipulate `var` ... When using `static` in the C I get: `static declaration of 'var' follows non-static declaration`, which actually makes sense to me ^^` I'm not sure if I unterstood correctly. – ElvisIsAlive Jul 11 '19 at 13:24
  • "static declaration of 'var' follows non-static declaration": you must define `var` once in the C file, and remove any reference to it (read: declaration) from the header. – virolino Jul 11 '19 at 13:31
  • Yes, that makes sense now ^^ I check again why I got an compile error manipulating var. ... classic user error, I assume ^^' Thanks for your responses! – ElvisIsAlive Jul 11 '19 at 13:37
  • You are welcome. Maybe you want to update the answer too? :D – virolino Jul 11 '19 at 13:42