This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C question: Why would one put 'static' variables in a header?

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?!

Parents
  • Although the use of static CAN be circumvented, as shown, it is still not conforming with the C spec:

    (1) An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.

    (2) In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal
    linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.


    3 If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage.

    Therefore, declaring static - by definition above - forces/restricts the identifier to be internal.

    Sorry about being nit-picky.

Reply
  • Although the use of static CAN be circumvented, as shown, it is still not conforming with the C spec:

    (1) An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.

    (2) In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal
    linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.


    3 If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage.

    Therefore, declaring static - by definition above - forces/restricts the identifier to be internal.

    Sorry about being nit-picky.

Children