Talk:Static variable

Untitled
There are a variety of variables, I don't know if it would be adequate to fit all of them on the one variable page. I feel that we would need stubs for each type of variable on the main page. While variable types should have their own pages elaborated with examples in a variety of languages. Below is some info on what should be covered. mkhjk Static Variables -exist from the time of creation until the termination of the program. -this can be used to implement history sensitive subprograms -because these variable are directly addressed they are more efficient -although with the static binding you loose the ability to call subprograms recursively -languages such as C and C++ allow you to specify a variable to be static with the static keyword Stack-Dynamic Variables -storage bindings are created during runtime -typing is bound at compile time -variable is allocated memory on the runtime stack -can be used to implement recursive subprograms Explicit Heap-Dynamic Variables -"nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instuctions" -"these variables are allocated and deallocated to the heap" -"can only be referenced through pointers" Implicit Heap-Dynamic Variables -"have the highest degree of flexibility" -"bound to heap storage only when they are assigned values." Serialloop (talk) 21:38, 1 May 2009 (UTC)

The above list is not complete -- it misses the distinction between lexical and dynamic variables as well as future variables, memoized variables, linear variables and so on. Any list you make will be incomplete. Pick areas that you know about and describe them rather than trying to enumerate a whole area that you do not know about.

If you want to talk about variables in general, then I suggest that you do that, and leave the rest for the disambiguation page.

Also, things like "have the highest degree of flexibility" are incoherent gibberish.

Merge proposed
Static variable is just a type of variable, there isn't that much to say about it - should be a section in Variable. Ripper234 17:29, 4 April 2006 (UTC)
 * Agreed. I removed the untrue claims and cleaned up the rest. There's not much left, so it should definitely merge. The Rod (&#9742; Smith) 19:54, 4 April 2006 (UTC)

I disagree with the merge proposal: I think this kind of article benefits from modularity and conciseness more than becoming part of a larger more unwieldy article. An article about variables in general is needed, but it is better as an overview referencing the detailed subtypes as separate pages for those wanting more detail. It also enables more accurate and relevant referencing from other articles. This is a problem of attention span and limited short term memory. I suggest we keep it modular for the same reason we prefer to program using small functions. User:copsewood 21 Dec 2006.

It would be nice to know if static local variables are stored somehow on the callstack like other local variables, in heap memory, or somewhere else entirely. --199.172.169.17 14:50, 18 July 2006 (UTC)

There is no callstack generally speaking, and more specifically speaking in C. You have auto, static, register and allocated storage (register being equivalent to auto, but unaddressable).

This being the case, the above question about 'where' is incorrect. Regard the semantics of these storage classes, do not impose broken preconceptions upon them.

Static Methods
I think someone should include a description of static methods, and the rules that apply - as in java -I'm currently confus-ed!

Static variables in Visual Basic
What to do about the fact that a static variable in Visual Basic and a local static variable in C are something else? Rp (talk) 20:12, 8 January 2009 (UTC)

what about performance?
do you get a faster function call with globale variables instead of static ones? are global ones faster? - maybe because static variables have to be pushed to stack and next time popped. —Preceding unsigned comment added by 62.202.30.116 (talk) 07:39, 11 March 2009 (UTC)

The above question is incorrect. The performance of 'global' or 'static' variables is not specified generally, nor in specific by C. Please correct your thinking. Also, where did you get this gibberish about 'static variables have to be pushed to stack'?

October 2009
I've just revised the article. Please check my work.

I carefully avoided mentioning C's default scope/extent rules, and removed a mention of the " " keyword. ISTM that this topic should be left to articles about C, keeping this article simpler and more generic.

Static local variables were available in ALGOL 60, where they were called "own variables". Neither ALGOL W nor ALGOL 68 had own variables. (The problem was that all three languages allowed arrays whose bounds were specified at run time, so you could declare an own array of n elements where n had a different value every time the function was called.) Does anyone think we should mention own variables?

Cheers, CWC 15:01, 17 October 2009 (UTC)

Static global vs. just global
What's the difference between static global variable and global variable in C? I'm referencing here the example code from the global variable article: Does it make any difference to declare global the following way: ? Thanks, --Abdull (talk) 21:43, 9 December 2009 (UTC)
 * According to ISO/IEC 9899:TC3, 6.2.2.5 (and 6.2.2.4), if no storage-class specifier is given, then linkage is as if declared with extern. Therefore,
 * has external linkage.
 * has external linkage.
 * has internal linkage.

Right? --Abdull (talk) 22:59, 30 January 2010 (UTC)


 * (Abdull @ 22:59 30 Jan is right.)
 * "Static variable" has two meanings, one applying to most programming languages and one applying only to C and related languages.
 * The general meaning is about lifetime (also known as extent): a static variable is associated with a particular region of memory for the program's entire lifetime. (In fact, the memory is statically allocated at link time.) Every global variable in C code is a static variable in this sense.
 * In C and related languages, "static variable" can also mean a variable defined with the static keyword. For variables declared or defined inside functions, using static makes them static in the general sense. For variables declared outside functions (ie., global variables), using static has no effect on lifetime but does affect visibility.
 * (C is designed to be useful, not to be easy to learn. For example, the rules for non-static global variables are much trickier.)
 * So our article has to deal with both of these meanings. I've just added a heading to separate the article into two sections.
 * I think the article currently has pretty much the right amount of detail static in C etc. Cheers, CWC 15:45, 7 February 2010 (UTC)

Section about C, C++, Objective-C
I've just done a substantial rewrite/reorganization of this section, adding a little detail. I don't want to add too much detail, lest the C/C++/ObjC content overwhelm the general content about static variables. So I decided not to mention:
 * that C has special rules about initial values for static variables
 * that the first call to a function with static local variables will see whatever values said variables were initialized with
 * that local static variables can be defined in, and so visible in, a nested scope rather than the whole function
 * the difference between declarations and definitions (which is immensely important with  variables)
 * that static global variables are not visible in inner scopes if hidden by variables with the same name

I want this article to be about the language-independent concept, not the C storage class, so I would rather not see the section grow much further.

Aside. I'm surprised to see that the C (programming language) article does not mention storage classes. If someone wants Wikipedia to cover C storage classes in more detail, it would be best to do so in that article or a spinoff article.

Cheers, CWC 15:26, 14 July 2010 (UTC)

C source file vs. C translation unit
Currently, this article states:
 * Every C variable defined as static has static lifetime and is not visible outside its own source file.

I think this statement is not precise/correct. I think the following statement is better:
 * Every C variable defined as static has static lifetime and is not visible outside its translation unit.

If there is consensus that the latter statement is better, I propose that it will be fixed in the article. --Abdull (talk) 16:31, 10 August 2010 (UTC)


 * Well, I wrote those words, and I much prefer "translation unit", so I've made the edit. (I also changed "defined as static" to "declared as static".) BTW, Translation unit (programming) is a nice article. Thanks again, Abdull. CWC 18:19, 11 August 2010 (UTC)
 * A thank you also goes to you for rewriting the article a few weeks ago! :) Now here is yet another question to ponder about: In C, if a variable is a global variable, then is this variable also always a static variable? I'd say yes. --Abdull (talk) 22:38, 12 August 2010 (UTC)
 * But  variables are global too. Technically, in C all   objects are global in extent (lifetime) but less-than-global in scope (visibility: only one translation unit or block), whereas   objects are global in both extent and scope.
 * While writing this, I found that Wikipedia has a good article (which I had not seen before) on scope (programming) and a good explanation (much better than I remembered) of scope and extent in the variable (programming) article. I have a to-do list somewhere with an item about improving Wikipedia's coverage of these issues ... now I can cross that item off &lt;grin&gt;. Cheers, CWC 10:23, 13 August 2010 (UTC)

May 2011
Following edits adding more details to the section about C family languages, I've revised that section. I've used a simple table to summarize the effects of static, extern and auto/register on lifetime and linkage, and added something about the two meanings of the term "static variable" in these languages. (I even said those two meanings are "easy to confuse".)

As I said above, I don't want this article to cover C's storage classes in detail (let alone C++'s), but it seems useful to provide more detail than my earlier edits did. What do other editors think? Corrections welcome. Cheers, CWC 07:53, 29 May 2011 (UTC)

x = x + 1
Any objections that we write  x++  instead?  x = x + 1  sounds so BASIC to me. ;) -andy 77.13.74.98 (talk) 06:51, 30 September 2012 (UTC)

Merge Static memory allocation here?
User has suggested merging our article on Static memory allocation into this one. Here's a place for other editors to comment.
 * Support. We should not have two articles for essentially the same topic. CWC 06:48, 14 January 2013 (UTC)


 * ✅. No one mentioned any reasons not to merge. So I merged the entire static memory allocation article into this static variable article. --DavidCary (talk) 21:39, 9 July 2016 (UTC)

is static variable assignment needed?
ie.

i.e. from linux kernel — Preceding unsigned comment added by 79.215.101.154 (talk) 11:56, 31 May 2014


 * C compilers are required to (arrange that) all static variables with no explicit initialization contain zeroes at the start of execution. (The C standards leave a little wiggle room about the meaning of "zeroes" here, but on modern systems statics default to all zero bits, meaning integer 0, float 0.0, pointer NULL etc.) Languages derived from C generally have the same rule; assembler does not. Hope this helps -- CWC 04:11, 1 June 2014 (UTC)

Date from at least ... 1958
Static variables date from at least FORTRAN II (1958) where they were known as 'COMMON' variables. 121.200.27.15 (talk) 06:45, 22 April 2023 (UTC)