What is the meaning of static?

  • A+
Category:Languages

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?

 


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:

  • Your first tab is a static global array of two uint8_t. It has static storage duration and internal linkage.
  • Your tab2 is a block scope array of two uint8_t. It has automatic storage duration and no linkage.
  • Your second tab is a static block scope array of two uint8_t. It has static storage duration and no linkage.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: