Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Static variable
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Scope== {{Unreferenced section|date=June 2023}} {{see also|Variable (computer science)#Scope and extent}} In terms of [[Variable (computer science)#Scope and extent|scope and extent]], static variables have extent the entire run of the program, but may have more limited [[Scope (computer science)|scope]]. A basic distinction is between a ''static global variable'', which has global scope and thus is in context throughout the program, and a ''[[static local variable]],'' which has local scope. A static local variable is different from a local variable as a static local variable is initialized only once no matter how many times the function in which it resides is called and its value is retained and accessible through many calls to the function in which it is declared, e.g. to be used as a count variable. A static variable may also have [[module scope]] or some variant, such as [[internal linkage]] in [[C programming language|C]], which is a form of file scope or module scope. ===Example=== An example of a static local variable in C: <syntaxhighlight lang=c> #include <stdio.h> void Func() { static int x = 0; // |x| is initialized only once across five calls of |Func| and the variable // will get incremented five times after these calls. The final value of |x| // will be 5. x++; printf("%d\n", x); // outputs the value of |x| } int main() { Func(); // prints 1 Func(); // prints 2 Func(); // prints 3 Func(); // prints 4 Func(); // prints 5 return 0; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)