Not Keil specific; one for the 'C' experts:
Why would one put 'static' variables definitions in a header?
eg, in a header file:
/* * TCO count to temperature conversion table. */ static erTcoTableStruct tcoTableOPUS1[] = { /*{TCO,Temp,} */ {1,-99}, {4,-45}, {5,-40}, {8,-35}, {11,-30}, {16,-25}, {22,-20}, {29,-15}, {37,-10}, {48,-5}, {61,0}, {78,5}, {99,10}, {124,15}, {153,20}, {188,25}, {227,30}, {270,35}, {315,40}, {365,45}, {420,50}, {481,55}, {549,60}, {625,65}, {710,70}, {805,75}, {910,80}, {1010,85}, {1060,88} };
AIUI, the whole point of so-called "header" files in 'C' is to share stuff between source files;
But the whole point of the 'static' keyword (at file scope) in 'C' is to make stuff private so that it is not visible to other modules - ie, not shared.
So I can't see why one would want to have 'static' definitions in a header?!
It is also potentially a waste of memory - every inclusion of the header file will instantiate a copy of the static variable in it.
I don't think that's just "potentially" - it's for certain!
Especially in cases like the example I showed - which is quite a large table!!
To show their C prowess?
We had a guy here a while ago that took one of my projects and put ALL the contents of every header file into one super large header file.
"Why?", I asked, "So I only have to edit one file of course" came the reply.
--- Now why didn't I think of that?
This was the same guy who had a function that returned "TRUE", "FALSE" and 2.
--- I'll never forget his C prowess!
and put ALL the contents of every header file into one super large header file
I know of at least one commercial product that has that (I did not write that part, though...).
There IS something called the C specification...but who the heck bothers to read (and understand) anymore?
www.open-std.org/.../n1124.pdf
Indeed - I have a copy.
But I still don't see why having static definitions in header files would be a useful thing to do.
What am I missing?
This could make some sense if each copy of the table had to be modified individually. The other possibility is that the author of the code didn't want to make the table 'global', so only the files that include the header can access it. Some kind of phobia of global variables. Either way, it looks strange.
Yes, I guess that could possibly be useful.
I still think it'd be confusing to have identically-named and identically-typed but distinct objects!
If I actually wanted to do that, I think I'd go for having an "initialiser" function which would apply the common initialisation to objects with meaningfully-distinct names!
"The other possibility is that the author of the code didn't want to make the table 'global', so only the files that include the header can access it"
But then they are not sharing the table - they each have their own distinct copy of it!
View all questions in Keil forum