Strange behavior with ++i twice in single std::cout <<
<p>Can someone please explain this to me. </p>

<p>below is the simplest of all codes, I am running a while loop 9 or 10 times and inside it I am displaying a variable incrementing it everytime the loop runs. But I do not understand the output or the warning message I get during compilation.</p>

<p><strong>code- with post-incremation</strong></p>

<pre><code>#include &lt;iostream&gt;

int main()
{

int i=0;

while(i&lt;9)
    std::cout&lt;&lt;i++&lt;&lt;"\t"&lt;&lt;i++&lt;&lt;std::endl;

return 0;
}
</code></pre>

<p>*<strong><em>output</em>*</strong>
1       0
3       2
5       4
7       6
9       8</p>

<p><strong>code2 -- I am pre-incrementing now</strong></p>

<pre><code>#include &lt;iostream&gt;

int main()
{

int i=0;

while(i&lt;9)
    std::cout&lt;&lt;++i&lt;&lt;"\t"&lt;&lt;++i&lt;&lt;std::endl;

return 0;
}
</code></pre>

<p><strong>output</strong></p>

<p>2       2
4       4
6       6
8       8
10      10</p>

<p><strong>Warning message on both instances</strong></p>

<blockquote>
  <p>main.cpp: In function ‘int main()’:</p>
  
  <p>main.cpp:37:39: warning: operation on ‘i’ may be undefined</p>
</blockquote>

<p><strong>The program shows correct behavior for the below code</strong></p>

<pre><code>#include &lt;iostream&gt;

int main()
{

int i=0;

while(i&lt;9)  {
    std::cout&lt;&lt;++i&lt;&lt;"\t";
    std::cout&lt;&lt;++i&lt;&lt;std::endl;

}

return 0;
 }
</code></pre>
