title: Reading variable numbers of space separated Integers through stdin as fast as possible
body: <p>I am trying to read the following input (Variable numbers of integers in a single line which are separated by spaces) through console using C in efficient manner. </p>
  
  <p><strong>Input:</strong></p>
  
  <pre><code>4
  3 1 2
  6 1 1 1 4
  17 2 3 2
  12
  </code></pre>
  
  <p>The number of input lines (here 4,  given in first line) are known but number of integers in a line are not known. 
  One of the similar question which I found is <a href="https://stackoverflow.com/questions/9599794/putting-numbers-separated-by-a-space-into-an-array">given here</a>, and solution is not working for me (I think I have understood this wrong).</p>
  
  <pre><code>while (i &lt; ARRAY_SIZE &amp;&amp; scanf("%d", &amp;a[i++] == 1);
  </code></pre>
  
  <p>I wrote one simple program using this solution:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main()
  {
  int i=0,ARRAY_SIZE=3,a[ARRAY_SIZE];
  while (i &lt; ARRAY_SIZE &amp;&amp; (scanf("%d", &amp;a[i++]) == 1));
  printf ("%d %d %d %d\n",a[0], a[1], a[2], a[3],i);
  }
  </code></pre>
  
  <p>For the above program 2 inputs and corresponding outputs are as follows:</p>
  
  <p><strong>Input-1:</strong></p>
  
  <pre><code>2 3\n
  4 5\n  
  </code></pre>
  
  <p><strong>Output-1:</strong></p>
  
  <pre><code>2 3 4 0 3
  </code></pre>
  
  <p><strong>Input-2:</strong></p>
  
  <pre><code>1 4 6 7 4 3 3\n 
  </code></pre>
  
  <p><strong>Output-2:</strong></p>
  
  <pre><code>1 4 6 0 3
  </code></pre>
  
  <p>But this is not what I was expecting from this program. Here I was trying to read only one line of integers, store them in an array and terminate as soon as one line is over so that If I want to read multiple line I am left with the option of using loop for those many lines (4 in the original input).</p>
  
  <p>It may happen that I am not able to understand above solution and trying to use it at inappropriate place. So guys please help me regarding taking space separated input of integers from console in an efficient fashion.</p>