The meaning of the "static" keyword in C depends on whether or not it appears within a brace-delimited block. Refer to the following code snippet.
int a; static int b; int func(void){ int c; static int d; ... }Variables 'a' and 'b' are declared outside of any brace-delimited blocks, therefore they both persist for the life of the program and are accessible from anywhere in the compilation unit. In this case, the "static" keyword has the effect of making variable 'b' local to the compilation unit. Variable 'a' can be accessed from any other compilation units that contain an extern declaration for it.
Variables 'c' and 'd' are declared within a brace-delimited block, therefore they are only accessible from within that block. In this case, the "static" keyword has the effect of making variable 'd' persist for the life of the program. Variable 'c' only persists while execution is within that brace-delimited block. There is no reason to expect variable 'c' to be stored at the same location for successive executions of the brace-delimited block.
C++ is consistent with C as far as the static keyword is concerned. However, in C++ the static keyword is largely deprecated and is in the language generally for backwards compatibility with C. There remain some uses of it, however.
No one has mentioned linkage so far, which is odd. Linkage comes in two flavours, external and internal. External linkage means the variable or function is accessible (shareable) from outside the compilation unit. Internal linkage means the variable/function is accessible only from inside the compilation unit.
So a static function cannot be called from outside the compilation unit but an external one can:
extern void MyFunction(int arg) { /* etc... */ }The function prototype is:
extern void MyFunction(int arg);The extern is the default. External functions (aka global functions) are callable by using the function prototype.
Static functions cannot be called from outside the compilation unit:
static void MyFunction(int arg) { /* etc... */ }The function prototype is:
static void MyFunction(int arg);You may place the static function prototype at the top of the file but the definition of the function must reside somewhere in the file. The above applies to variables.
extern int value;This variable is accessible from another compilation unit by:
extern int value;Static variables have internal linkage and are accessible only in the current scope. If the static variable is inside a function, it is accessible only from inside the function. Here the static keyword also causes the local variable to persist after the function completes execution. It's kind of like a local global.