what does static char do?

I've stumbled accross this gem a while ago and been using in some of my setups without really knowing what it does.....

static char out[50];

I think its creating a static character called "out" which I am then using as the output in my loop.

What's the difference between a char and a static char and what does the [50] do?

Thanks

char just a character, example

char msg[30];

i name char as msg , i sending message per second limited 30 time.

static makes the variable persistent, even when it goes out of scope. It is like a global, but its visibility is limited to the block in which it was defined. For example, a variable defined within a function normally loses its value, and ceases to exist as soon as the function returns. Defining that variable as static allocates a permanent memory location for it, and it retains its value between calls to the function.

The [50] indicates a 50-element array of char's is being defined.

Regards, Ray L.

Makes sense. Thanks Ray

The 'static' keyword has three uses that I know of -- there could be more:

  1. When applied to an external variable (i.e. not defined within any function), it restricts the scope of that variable to the file ("compilation unit" to the more pedantic folks) in which it is defined. Without the 'static' keyword, external variables have global scope.

  2. When applied to a local variable (i.e. defined within a function), it causes the value of that variable to persist between calls to the function.

  3. When applied to a class member, it defines it as a class variable. This means there is only one instance of it for the entire class.

Your #1 and #2 are both basically special cases of the same rule - static restricts visibility (scope) of a variable to the current scope. When used inside a {} pair. the scope is within that {} pair. Outside any {} pair, it CAN restrict scope to the current source file but that is a dicey rule in Arduino-world, due to the funky auto-build process.

Regards, Ray L.

RayLivingston: Your #1 and #2 are both basically special cases of the same rule - static restricts visibility (scope) of a variable to the current scope. When used inside a {} pair. the scope is within that {} pair. Outside any {} pair, it CAN restrict scope to the current source file but that is a dicey rule in Arduino-world, due to the funky auto-build process.

If a variable is defined between a '{}' pair, its scope is ALWAYS confined between that pair. Adding 'static' doesn't change that. Rather, it causes its value to persist between times that execution passes through that scope. Stated another way 'static' extends the lifetime of a variable defined between a '{}' pair to the execution period of the whole program. However, it doesn't affect the scope in ANY WAY.

A variable not defined between a '{}' pair always has the lifetime of the program and, by default, has global scope. Adding 'static' restricts its scope to that of the compilation unit. I'll drop the word 'file' from that definition because (as you pointed out) of Adruino's '.ino' file combining habit. That's one of the reasons I don't use that feature. You won't find it used in serious FW development in industry. Also, I'd venture a guess that it's not taught in college-level programming courses either (although that was a LONG TIME ago for me). I prefer to have only one '.ino' file (at most) along with '.cpp' / '.h' files as required.

I did think of two other uses of 'static'. They are similar to my original ones but related to functions:

  1. When applied to a function, 'static' restricts the scope of that function to the current compilation unit. By default, functions have global scope.

  2. When applied to a method defined in a class, 'static' makes it a 'class method'. This means there is only one instance of it for the entire class. This use reduces to #3 above if you assume a 'class member' can be a variable or a method.