what is the matter with this static variable?

Hi,everybody watching my topic here.
My question might be quite simple,here it comes.
I have two files ,one is named 'a.h',and the other 'a.c' and below is the code
a.h
1
2
3
4
#ifndef _A_H
#define _A_H
static int internalUsage;
#endif 


a.c
1
2
3
#include "a.h"

internalUsage = 100;

And i key in the command "gcc a.c" ,then error message shows up which drove me carzy.

F:\UltraEditFiles\a.c:3: error: non-static declaration of 'internalUsage' follows static declaration
F:\UltraEditFiles\a.h:3: error: previous declaration of 'internalUsage' was here
F:\UltraEditFiles\a.c:3: warning: data definition has no type or storage class

Any suggestions or advice is greatly appreciated.
What are you trying to achieve? If you want an internal used integer just do not put it into the header but as static integer into the c-file.

1
2
3
4
5
// a.h
// nothing mentioned about "internalUsage"

// a.c
static int internalUsage = 100;


If you try to have the same variable accessed in different places, put it as extern in the header and non-extern (and non-static!) into the c-file.

1
2
3
4
5
// a.h
extern int internalUsage;

// a.c
int internalUsage = 100;



And finally, if you want every c-file that includes the header to have an own internal integer value seperated from all the other c-files that include the same header and want your a.c have this value to a default of 100 (whereas the other c-files that include a.h have an undefined default value), then leave your header as it is but put a "static int" before the definition in the c-file. This is a rather seldom use case... in fact, I never saw this in the wild and just invented it.

1
2
3
4
5
// a.h
static int internalUsage;

// a.c
static int internalUsage = 100;


Ciao, Imi.
Last edited on
Get it .thanks a lot ,what an amazing place!
Topic archived. No new replies allowed.