<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://socialledge.com/sjsu/index.php?action=history&amp;feed=atom&amp;title=Meaning_of_Static</id>
		<title>Meaning of Static - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://socialledge.com/sjsu/index.php?action=history&amp;feed=atom&amp;title=Meaning_of_Static"/>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=Meaning_of_Static&amp;action=history"/>
		<updated>2026-05-12T03:37:41Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.27.1</generator>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=Meaning_of_Static&amp;diff=3037&amp;oldid=prev</id>
		<title>Preet: Created page with &quot;The '''static''' variable has so many meanings that it is best to describe them by example:  === static in Header File === '''Every file''' that includes '''my_file.h''' will ...&quot;</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=Meaning_of_Static&amp;diff=3037&amp;oldid=prev"/>
				<updated>2013-04-17T18:08:11Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;The &amp;#039;&amp;#039;&amp;#039;static&amp;#039;&amp;#039;&amp;#039; variable has so many meanings that it is best to describe them by example:  === static in Header File === &amp;#039;&amp;#039;&amp;#039;Every file&amp;#039;&amp;#039;&amp;#039; that includes &amp;#039;&amp;#039;&amp;#039;my_file.h&amp;#039;&amp;#039;&amp;#039; will ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The '''static''' variable has so many meanings that it is best to describe them by example:&lt;br /&gt;
&lt;br /&gt;
=== static in Header File ===&lt;br /&gt;
'''Every file''' that includes '''my_file.h''' will receive its '''own, independent''' copy of my_var and my_arr[128].  This copy is not shared between two files that include '''my_file.h'''.  Use this strategy to create multiple copies of these variables at compile time, but avoid it in general because it is a big mess to maintain.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/* my_file.h */&lt;br /&gt;
static int my_var = 1;&lt;br /&gt;
static int my_arr[128] = { 0 };&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== static in Source File ===&lt;br /&gt;
When static functions or variables are defined in a source file, then these are inaccessible members for anyone else, and thus private members for this source code file.  No one else can access them and other source file can have their own static members with the same name and there will not be any compiler issue.  If you declare multiple '''hello_world()''' functions in multiple source files without the '''static''' keyword, then you will get compiler error for multiple definitions of the same function.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/* my_file.c */&lt;br /&gt;
static int my_var = 0;&lt;br /&gt;
static void hello_world(void)&lt;br /&gt;
{&lt;br /&gt;
    /* Your code */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== static inside Functions ===&lt;br /&gt;
Static members of a function are like global variables, except that they are private to the function.  They are global in a sense that they are not re-declared each time the function is called.  The static variables are declared and initialized once, and their value is preserved across multiple function calls.  In the example below, we will see '''i = 1''' and '''i = 2''' and so forth each time the function is called.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
void hello_world(void)&lt;br /&gt;
{&lt;br /&gt;
    static int i = 0;&lt;br /&gt;
    i++;&lt;br /&gt;
    printf(&amp;quot;i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
void init_once(void)&lt;br /&gt;
{&lt;br /&gt;
    // This will only occur once.&lt;br /&gt;
    static int *my_var_ptr = malloc(sizeof(int));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== static inside Classes ===&lt;br /&gt;
In C++, the static member is shared between ALL instances of the class.  They may be tricky to access because each class doesn't have its own variable and therefore is inaccessible by this-&amp;gt; pointer.  The static variable must be declared outside of the class scope in the source file.&lt;br /&gt;
The second case is a static variable inside of a function.  This static variable is again shared between ALL instances of the class.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
class foo&lt;br /&gt;
{&lt;br /&gt;
    public:&lt;br /&gt;
        foo() { ++num_instances; }&lt;br /&gt;
        void hello(void)&lt;br /&gt;
        {&lt;br /&gt;
            /* i is shared between all instances, therefore each time hello()&lt;br /&gt;
             * is called, even from multiple instances, it increments a single&lt;br /&gt;
             * variable i&lt;br /&gt;
             */&lt;br /&gt;
            static int i = 0;&lt;br /&gt;
            i++;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
    private:&lt;br /&gt;
        static int num_instances;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Preet</name></author>	</entry>

	</feed>