1

say I have 4 files.

file1.h
file1.c
file2.h
file2.c 

in file1.h I have a variable :

static short mode=0;

in file1.c:

#include "file1.h"
...............
if ( mode ==0)
  do stuff
else {
   do something else 
}

the problem I have is that I can only change mode value in file2.c

in file2.h I include file1.h and in the source file2.c, I change the value of to 1 but this has no influence on it in file1.c

2
  • I think it might need to be declared 'extern', and defined in one of the c files...not sure though.
    – sje397
    May 20, 2015 at 8:20
  • "in file2.h I include file1.h" You should not do that, if your includes are not protected enough from multiple inclusion (with #ifdefs and such) it can lead to quite a few problems
    – Eregrith
    May 20, 2015 at 8:21

5 Answers 5

3

When you write a line like

static short mode=0;

in a .h file and the .h file is included in multiple .c files, you effectively create multiple short mode variables, one in each .c file that ends up including the .h file.

If you want to have one variable, make it extern, make sure to define it in only one .c file.

in file1.h:

// Just the declaration in the .h file.
extern short mode;

in file1.c:

// The definition in only one .c file.
short mode = 0;
2

The whole point of having a variable declared as static is to reduce its scope and make it inaccessible to other files. This is known as private encapsulation and is good programming practice.

The opposite of private encapsulation is known as spaghetti coding, where you declare a variable as global, with the extern keyword. This is very bad programming practice (unless in some cases where the variable is declared const, which isn't the case here).

Under no circumstances should you attempt to rewrite good code based on private encapsulation into bad code based on spaghetti.

Also you should never define variables in header files, because that never makes any sense. A header file is just a description of the interface which is implemented in its corresponding c file, it should not implement anything (even though C allows one to do all kinds of stupid and crazy things).

What you should do is:

file1.h

short get_mode (void);
void  set_mode (short m);

file1.c

#include "file1.h"

static short mode = 0;

short get_mode (void)
{
  return mode;
}

void set_mode (short m)
{
  mode = m;
}

some_other_file.c

#include "file1.h"

short mode = get_mode();
do_stuff_with(mode);

This is how you design programs properly, period. No matter which programming language that is used. Do not listen to anyone recommending extern or other such nonsense!

10
  • Mandatory comment from some 1980s dinosaur that will follow: "But this adds several CPU ticks of overhead code." Mandatory answer will follow: "No, all compilers on the market are perfectly capable of inlining code, and have been so since the mid 90s".
    – Lundin
    May 20, 2015 at 8:42
  • Nice. Hats off to you :)
    – R Sahu
    May 20, 2015 at 8:52
  • 1
    Hang on. You complain that global variables are bad programming practice, but you still have one! It's just that you've hidden it. All the normal global variable problems, such as lack of reentrancy and lack of thread safety, still apply. May 20, 2015 at 10:13
  • @JackWhitham Global variables are variables that are accessible globally everywhere in the project, hence global. This example has a local file scope variable which is not the same thing. The main problems with globals are avoided: spaghetti program design and cluttering of the global namespace. But indeed any static variable will become problematic in multi-threaded systems and therefore needs to be protected by semaphores.
    – Lundin
    May 20, 2015 at 10:52
  • 1
    The arguments you give are not valid, imo: "spaghetti program design" is not prevented at all (if assignments are replaced by get or set, how does that improve program structure?); and "cluttering of the global namespace" is, if anything, increased because the getter and setter introduce two names instead of just one, the variable name. May 20, 2015 at 11:38
1

If you have static in .h file defined and including that .h file in .c files you are basically defining static variable in each .c file and they are all different objects.

You have to do

extern short mode;

in some .h file and include this .h in whichever .c file you need.

Once the variable is declared in some .h file you need to define it somewhere.. I mean in some .c file.

short mode;
1
0

Move the variable declaration to file1.c and get rid of static:

  short mode=0;

Add an extern definition to file1.h:

  extern short mode;
7
  • No, don't add spaghetti to a properly designed program! Write setter/getter functions.
    – Lundin
    May 20, 2015 at 8:26
  • @Lundin And C is, frankly, the wrong language for that approach. May 20, 2015 at 8:30
  • 1
    @PeterSchneider Good program design is, frankly, completely unrelated to which programming language you are using. Programming in C is certainly not an excuse for writing crap code!
    – Lundin
    May 20, 2015 at 8:39
  • 1
    @Lundin Bad code comes in many forms - overengineering is one of them. It's best to use programming techniques that are appropriate for the situation. May 20, 2015 at 8:52
  • @JackWhitham There is an overwhelming amount of research and widely-recognized coding standards that all say that global variables are bad. Regardless of programming language. There exists very few if any cases where (non const) global variables are the correct solution to a problem. Even if they seem to be because the use is limited and the code is simple, further development/maintenance of that code which seemed simple at first, may add complexity and need for even more variables. And if you used global variables in the original code, then that is the moment when bad code rot begins.
    – Lundin
    May 20, 2015 at 9:01
-1

That is the semantics of static in this context: It creates a separate variable in each source file.

For a common variable which spans several source files you need extern declarations in each source file which wants to use it -- typically through a header --, and one definition in one of the source files.

On a general note, I can recommend the C FAQ (http://c-faq.com/). There is a section about declarations and definitions, http://c-faq.com/decl/decldef.html.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.