Close.
Anything at global scope (ie: outside a function) is static by default.
eg:
//main.c
int myVar; // global, and static
int main(void) {
...
return 0;
}
//morecode.c
extern int myVar; //other C files can see and use this global/static variable
However, if you explicitly declare something at the global scope as static, not only is it static, but it is only visible inside that file. Other files can't see it.
//main.c
static int myVar; // global, and static
int main(void) {
...
return 0;
}
//morecode.c
extern int myVar; // compiler error; "myVar" can only be seen by
// code in main.c since it was explicitly
// declared static at the global scope
Also, nothing is "extern" by default. You typically use extern to access global variables from other files, provided they weren't explicitly declared static as in the example above.
const only implies that the data cannot change, regardless of it's scope. It does not imply extern, or static. Something can be "extern const" or "extern", but "extern static" doesn't really make sense.
As a final example, this code will build on most compilers, but it has a problem: myVar is always declared "extern", even in the file that technically creates it. Bad practice:
//main.c
extern int myVar; // global, and static, but redundant, and might not work
// on some compilers; don't do this; at least one .C file
// should contain the line "int myVar" if you want it
// accessible by other files
int main(void) {
...
return 0;
}
//morecode.c
extern int myVar; //other C files can see and use this global/static variable
Finally, you might want to cover this post on the various levels of scope if you are not already familiar with them. It will likely be helpful and informative to you. Good luck!
Terminology definition - Scope in C application
The person who answered this question of mine on scope did a good job, in my opinion.
Also, if you declare something static within a function, the value remains between function calls.
eg:
int myFunc(int input) {
static int statInt = 5;
printf("Input:%d statInt:%d",input,statInt);
statInt++;
return statInt;
}
int main(void) {
myFunc(1);
myFunc(5);
myFunc(100);
return 0;
}
Output:
Input:1 statInt:0
Input:5 statInt:1
Input:100 statInt:2
Note that the use of a static variable within a function has a specific and limited number of cases where they are useful, and generally aren't a good idea for most projects.