title: Initialization vs Assignment in C
body: <p>My instructor recently told that array initialization in C happens in two ways, namely:</p>
  
  <ol>
  <li>Manually like <code>int a[5]={1,2,3,4,5};</code></li>
  <li>Using <code>scanf()</code> like <code>int a[5], i; for(i=0;i&lt;5;i++) scanf("%d", &amp;a[i]);</code></li>
  </ol>
  
  <p>In my opinion the second "way" is a way of <em>assignment</em> and not <em>initialization</em>. So I decided to check what people over here have to say about this. I stumbled upon <a href="https://stackoverflow.com/questions/3779707/c-sharp-variable-initializations-vs-assignment">this</a> post where one answer claims:</p>
  
  <blockquote>
    <p>If all you are asking about is terminology (*which isn't really clear
    from your question), "Initialization" of a variable is, literally, the
    first time a value is assigned to it. This term comes from the fact
    that you are giving the variable it's "initial" value.</p>
    
    <p>This should (obviously) happen before it is used the first time.</p>
    
    <p><code>int x=5;</code> is a declaration and an initialization, and is really just
    convenient shorthand for</p>
    
    <p><code>int x; x=5;</code></p>
  </blockquote>
  
  <p>If I'm to follow what this particular answer claims, then the second way of "initialization" is correct, because no value was assigned prior to the <code>scanf()</code> statement. However, thanks to my knowledge on static variables a new doubt arises in my mind. Consider the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void first_way(){
      static int x[2]={1,2};
      printf("first_way called %d time(s)\n",++x[0]);
  }
  
  void second_way(){
      int i;
      static int x[2];
      for(i=0;i&lt;2;i++)scanf("%d",&amp;x[i]);
      printf("second_way called %d time(s)\n",++x[0]);
  }
  
  int main(void){
      int i;
      for(i=0;i&lt;3;i++)
          first_way();
      printf("\n#######\n");
      for(i=0;i&lt;3;i++)
          second_way();
      return 0;
  }
  </code></pre>
  
  <p>Its output is like this:</p>
  
  <pre><code>first_way called 2 time(s)
  first_way called 3 time(s)
  first_way called 4 time(s)
  
  #######
  1 2
  second_way called 2 time(s)
  1 2
  second_way called 2 time(s)
  1 2
  second_way called 2 time(s)
  </code></pre>
  
  <p>This output again leads me to think of <code>scanf()</code> version more like an <em>assignment</em> version rather than <em>initialization</em> even though no value to elements of <code>x[]</code> were assigned before the <code>scanf()</code> statement. Back to square one.</p>
  
  <p>So, is the second version really an initialization like my instructor claims or merely an assignment (which is what I believe)?</p>
  
  <h1>Edit:</h1>
  
  <p>After someone pointed out, I feel my <code>static</code> array example is a poor one as <code>static</code> variables are implicitly initialized to 0 no matter what. Then again, someone else pointed me towards <code>const</code> variables.</p>
  
  <p>Consider <code>const int x = 2;</code> Here one can initialize <code>x</code>, but cannot assign any value to it after initialization. But this is conflicting with the answer that claims (I quote it again):</p>
  
  <blockquote>
    <p><code>int x = 5;</code> is a declaration and an initialization, and is really just
    convenient shorthand for <code>int x; x=5;</code></p>
  </blockquote>
  
  <p>So, after all this, does the <code>scanf()</code> version qualify as an initializer?</p>