Engine
Engine

Reputation: 5432

Issues with a static variable in C

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

Upvotes: 1

Views: 546

Answers (5)

R Sahu
R Sahu

Reputation: 206717

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;

Upvotes: 3

Lundin
Lundin

Reputation: 214860

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!

Upvotes: 2

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16112

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.

Upvotes: -1

Gopi
Gopi

Reputation: 19874

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;

Upvotes: 1

Jack Whitham
Jack Whitham

Reputation: 609

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;

Upvotes: 0

Related Questions