Hello Everybody
I found the description for the keyword static here Static Variables in C - GeeksforGeeks which I do not understand for static global variable
Any help would be appreciated
Hello Everybody
I found the description for the keyword static here Static Variables in C - GeeksforGeeks which I do not understand for static global variable
Any help would be appreciated
We have a recent thread that you might want to read: global versus static variables
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It say when static use inside in function it is only visible only to the function in which it is defined. and when static use outside the function it say variable will visible to all the functions in the current source file
It means there is no difference between auto global variable (declare outside of function) and static global variable (declare outside of function) because both are visible to all the function in the current source file and both variable will alive until program is running
If you only have one source file, sure. Most non-trivial programs consist of many source files so the difference does matter.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This may not be accurate, but my understanding is that marking a global variable in a source file as static means that while the variable is indeed global, it's global only to the functions in that particular source file and isn't visible to other files. One use for this could be sharing global state among a limited subset of functions as part of a program.
So, think of the keyword "static" - at least as it applies to global variables - as meaning "this source file only".
Last edited by TheMuffenMann; 04-17-2020 at 11:50 AM.
When we refer to a "global" (Non-static) variable, it is a variable that will be visible to any function in any file as long as there is an "extern" declaration, usually in a header file. The variable symbol is passed to the linker to be resolved by the linker. You would not declare a variable in a header file as both "extern" and "static"!
A "static" variable defined outside of any function in one .c file, is visible to any function defined after the definition of the "static" variable in that one .c file. The symbol is not passed to the linker and all references to the static variable are resolved by the compiler.
I would avoid calling a file level static variable, global!