0

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?

Shani Gamrian
  • 345
  • 1
  • 4
  • 18

1 Answers1

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
  • 1
    Try 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