I have a static function defined in a c file that uses global static variables of the file. If I call the function from another file and I define the same static global variables but with different values, will it use the values from the original file or from the other file? If not, is there a way to use global parameters in a function I'm calling from different files without receiving them as inputs?
Asked
Active
Viewed 1,595 times
0
-
4global *static* variables are file-local... so I guess the answer is "no, it wouldn't", and "no"... and where's the code? – Antti Haapala -- Слава Україні May 22 '17 at 12:04
-
3Furthermore a function with static visibility cannot be called from another file. – Lanting May 22 '17 at 12:04
-
1Have a look at this: http://stackoverflow.com/questions/3684450/what-is-the-difference-between-static-and-extern-in-c – Bathsheba May 22 '17 at 12:05
-
1Possible duplicate of [How do I use extern to share variables between source files?](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) – Badda May 22 '17 at 12:05
-
"global" and "static" is actually the same thing. Do you mean `static` qualified? – too honest for this site May 22 '17 at 12:51
1 Answers
2
Static variables defined at the outermost level of a source file have file scope, i.e.: they are only visible in that file.
As an example, if you have a source file foo.c
:
static int var;
and another one bar.c
:
static int var;
There are two different copies of a variable with the name var
. Each copy is only visible in the file in which it is defined.

JFMR
- 23,265
- 4
- 52
- 76
-
1Try to avoid naming like this, name them foo_var and bar_var. You will thank yourself when debugging. – Jeroen3 May 22 '17 at 12:34