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?

5
  • 4
    global static variables are file-local... so I guess the answer is "no, it wouldn't", and "no"... and where's the code? Commented May 22, 2017 at 12:04
  • 3
    Furthermore a function with static visibility cannot be called from another file. Commented May 22, 2017 at 12:04
  • 1
    Have a look at this: stackoverflow.com/questions/3684450/… Commented May 22, 2017 at 12:05
  • 1
    Possible duplicate of How do I use extern to share variables between source files? Commented May 22, 2017 at 12:05
  • "global" and "static" is actually the same thing. Do you mean static qualified? Commented May 22, 2017 at 12:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Try to avoid naming like this, name them foo_var and bar_var. You will thank yourself when debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.