Reputation: 444
Consider following code:
static uint8_t tab[] = {1, 2};
void foo() {
uint8_t tab2[] = {1, 2};
}
I have here two arrays one is with static
keyword the second is within function. How should I properly name each of them? Because those two are static. I know the difference that tab
array will exist forever unlike tab2
which exists only when executing function foo()
. If tab2
is static array which dies after function exit shouldn't it be called another? What are proper names of those arrays accroding to programming theroy?
Another example:
void bar() {
static uint8_t tab[] = { 1, 2 };
}
What is proper name according to programming-theory of such variable? "static of static" array?
Upvotes: 0
Views: 427
Reputation: 70391
The static
keyword means static storage duration and internal linkage (unless at block scope, where it means static storage duration and no linkage as block-scoped things are never linked).
Your tab2
is not static, it has automatic storage duration and no linkage. (There is even a keyword for that, auto
, which is implied and not normally used however. Don't confuse this with C++, which has "recycled" that keyword for a completely different meaning.)
What identifier you give your variables -- e.g. tab
or tab2
-- is up to you. But what they are is very well defined by the language:
tab
is a static global array of two uint8_t
. It has static storage duration and internal linkage.tab2
is a block scope array of two uint8_t
. It has automatic storage duration and no linkage.tab
is a static block scope array of two uint8_t
. It has static storage duration and no linkage.Upvotes: 6
Reputation: 385405
There seems to be a misunderstanding here. That's not entirely your fault, seeing as "static" means at least two different things in C.
Your tab2
is not static. It does not have static storage duration, nor is it in any way related to static storage. It's just an array. It's a local variable that is an array.
Your tab
is a global array which gives it static storage duration, and has internal linkage due to the keyword static
. If it were within a function, it would be "function-static".
If I were you, I wouldn't worry too much about terminology; instead, worry about making your program do what you want it to do!
Upvotes: 2
Reputation: 60163
static
on filescope declarations means "local to this translation unit".
Example:
static int variable=42;
static int function(){return 0;}
//won't be visible in other translation units
static
on block-scoped declarations means "with static storage duration":
Example:
uint8_t *bar() {
static uint8_t tab[] = { 1, 2 };
return &tab[0];
//ok with static but pretty much always an error otherwise
}
int how_many_times_i_ran(){ static unsigned x; return x++; }
static
inside the brackets of "array-typed" function parameters means the integer expression that follows denotes the minimum number of elements that the pointer must point to.
void take3(int Three[static 2+1]);
void call()
{
take3((int[3]){0}); //OK
take3(0); //undefined
take3((int[2]){0}); //undefined
}
Upvotes: 1
Reputation: 215305
Clearing out terms:
1) static uint8_t tab[] = {1, 2};
2) void foo() { uint8_t tab2[] = {1, 2}; ...
Scope:
1) Is a variable declared at file scope. 2) is a variable declared at local scope. Scope determines where a variable is accessible.
Storage duration:
main()
is called, either to zero or to a specific value.All variables declared at file scope has static storage duration.
All variables declared as static
has static storage duration.
All variables declared at local scope, without static
, has automatic storage duration.
(Some other cases exist for extern
etc but that's not relevant here. There is the term linkage, which I won't address here.)
Knowing this, then "because those two are static" doesn't make sense. They have a statically set and fixed size, but that's all they have in common.
Regarding naming, that's subjective and up to your own coding standard. Give them intuitive names explaining what they contain, rather than what type they are.
Some older coding standards liked to regard static
-declared file scope variables as private variables and therefore used a _
prefix to indicate this. But this is bad practice, because it collides with naming rules of the C standard (and the POSIX standard).
Upvotes: 2
Reputation: 149185
C has a full bunch of classes (name is mine) of variables, with variations of scope (for what part of code the variable can be accessed) and duration.
At file level (outside of any function):
Inside a function or a bloc:
In addition, a variable can be declared as extern
. In that case, the variable is only declared, and has to be defined elsewhere with the same declaration in the same compilation unit or in another one.
To be exhaustive, C has another category for duration: allocated. It is used for objects that are created through malloc and that persist until they are explicitely freed. But beware: the programmer is responsable to keep a pointer to the object with the normal scope rules to be able to free it.
Upvotes: 1
Reputation: 31459
What are proper names of those arrays accroding to programming theory?
Naming identifiers is not a part of programming theory. How you should name them is a matter of best practice, conventions, company standards and such. And how this should be done is very opinion based.
Upvotes: 1