Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the below example <code>static int count1 = 0;</code> is initialization(assigning values at declaration). And <code>count1++;</code> is equivalent to <code>count1 = count1 + 1;</code> and this is assignment not initialization. </p> <p>While calling <code>count</code> function first time <code>static int count1 = 0;</code> will be executed and 0 will be assigned, and also <code>count1++</code> will also assign <code>1</code> to the staitc variable <code>count1</code>. While calling <code>count</code> function second time <code>static int count1 = 0;</code> will not be executed and <code>count1++</code> will be executed and <code>2</code> will be assigned to <code>count1</code>.</p> <pre><code>void count(void) { static int count1 = 0; int count2 = 0; count1++; count2++; printf("\nValue of count1 is %d, Value of count2 is %d", count1, count2); } </code></pre> <p>Initialization means assigning a value to a variable during the declaration of that variable(during intial stage). Assigining values after declaration is just assignment not initialization.</p> <p>For a static variable initialization will be done only once, but the assignment can be done for any number of times.</p> <p>Now in your first program, you are doing <code>a=15;</code>, you are thinking like <code>=</code> operation is not possible in 2nd time and <code>++</code> operation is possible for many times for a static variable.</p> <p>In your program <code>a=15;</code> can be written as below also with <code>++</code> operation.</p> <pre><code>for(j = 0; j = 15; j++) { a++; } </code></pre> <p>So assignment (<code>=</code>) operation is valid on a static variable even in second time also.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload