<p>In C, what is the difference between using <code>++i</code> and <code>i++</code>, and which should be used in the incrementation block of a <code>for</code> loop?</p>

[{'answer_id': 24856, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 6, 'selected': False, 'text': "<p><code>++i</code> increments the value, then returns it.</p>\n\n<p><code>i++</code> returns the value, and then increments it.</p>\n\n<p>It's a subtle difference.</p>\n\n<p>For a for loop, use <code>++i</code>, as it's slightly faster. <code>i++</code> will create an extra copy that just gets thrown away.</p>\n"}, {'answer_id': 24858, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 11, 'selected': True, 'text': '<ul>\n<li><p><code>++i</code> will increment the value of <code>i</code>, and then return the incremented value.</p>\n\n<pre><code> i = 1;\n j = ++i;\n (i is 2, j is 2)\n</code></pre></li>\n<li><p><code>i++</code> will increment the value of <code>i</code>, but return the original value that <code>i</code> held before being incremented.</p>\n\n<pre><code> i = 1;\n j = i++;\n (i is 2, j is 1)\n</code></pre></li>\n</ul>\n\n<p>For a <code>for</code> loop, either works. <code>++i</code> seems more common, perhaps because that is what is used in <a href="https://en.wikipedia.org/wiki/The_C_Programming_Language" rel="noreferrer">K&amp;R</a>.</p>\n\n<p>In any case, follow the guideline "prefer <code>++i</code> over <code>i++</code>" and you won\'t go wrong.</p>\n\n<p>There\'s a couple of comments regarding the efficiency of <code>++i</code> and <code>i++</code>. In any non-student-project compiler, there will be no performance difference.  You can verify this by looking at the generated code, which will be identical.</p>\n\n<p>The efficiency question is interesting... here\'s my attempt at an answer:\n<a href="https://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i">Is there a performance difference between i++ and ++i in C?</a></p>\n\n<p>As <a href="https://stackoverflow.com/users/2150/on-freund">@OnFreund</a> notes, it\'s different for a C++ object, since <code>operator++()</code> is a function and the compiler can\'t know to optimize away the creation of a temporary object to hold the intermediate value.</p>\n'}, {'answer_id': 24874, 'author': 'OysterD', 'author_id': 2638, 'author_profile': 'https://Stackoverflow.com/users/2638', 'pm_score': 5, 'selected': False, 'text': "<p>The reason <code>++i</code> <em>can</em> be slightly faster than <code>i++</code> is that <code>i++</code> can require a local copy of the value of i before it gets incremented, while <code>++i</code> never does. In some cases, some compilers will optimize it away if possible... but it's not always possible, and not all compilers do this.</p>\n\n<p>I try not to rely too much on compilers optimizations, so I'd follow Ryan Fox's advice: when I can use both, I use <code>++i</code>. </p>\n"}, {'answer_id': 107181, 'author': 'Andy Lester', 'author_id': 8454, 'author_profile': 'https://Stackoverflow.com/users/8454', 'pm_score': 6, 'selected': False, 'text': '<p>Please don\'t worry about the "efficiency" (speed, really) of which one is faster.  We have compilers these days that take care of these things.  Use whichever one makes sense to use, based on which more clearly shows your intent.</p>\n'}, {'answer_id': 5917266, 'author': 'dusktreader', 'author_id': 642511, 'author_profile': 'https://Stackoverflow.com/users/642511', 'pm_score': 4, 'selected': False, 'text': '<p>The effective result of using either in a loop is identical.  In other words, the loop will do the same exact thing in both instances.</p>\n\n<p>In terms of efficiency, there could be a penalty involved with choosing i++ over ++i.  In terms of the language spec, using the post-increment operator should create an extra copy of the value on which the operator is acting.  This could be a source of extra operations.</p>\n\n<p>However, you should consider two main problems with the preceding logic.</p>\n\n<ol>\n<li><p>Modern compilers are great.  All good compilers are smart enough to realize that it is seeing an integer increment in a for-loop, and it will optimize both methods to the same efficient code.  If using post-increment over pre-increment actually causes your program to have a slower running time, then you are using a <em>terrible</em> compiler.</p></li>\n<li><p>In terms of operational time-complexity, the two methods (even if a copy is actually being performed) are equivalent.  The number of instructions being performed inside of the loop should dominate the number of operations in the increment operation significantly.  Therefore, in any loop of significant size, the penalty of the increment method will be massively overshadowed by the execution of the loop body.  In other words, you are much better off worrying about optimizing the code in the loop rather than the increment.</p></li>\n</ol>\n\n<p>In my opinion, the whole issue simply boils down to a style preference.  If you think pre-increment is more readable, then use it.  Personally, I prefer the post-incrment, but that is probably because it was what I was taught before I knew anything about optimization.</p>\n\n<p>This is a quintessential example of premature optimization, and issues like this have the potential to distract us from serious issues in design.  It is still a good question to ask, however, because there is no uniformity in usage or consensus in "best practice."</p>\n'}, {'answer_id': 6945779, 'author': 'Scott Urban', 'author_id': 746993, 'author_profile': 'https://Stackoverflow.com/users/746993', 'pm_score': 3, 'selected': False, 'text': "<p>I assume you understand the difference in semantics now (though honestly I wonder why\npeople ask 'what does operator X mean' questions on stack overflow rather than reading,\nyou know, a book or web tutorial or something.</p>\n\n<p>But anyway, as far as which one to use, ignore questions of performance, which are\nunlikely important even in C++.  This is the principle you should use when deciding\nwhich to use:</p>\n\n<p>Say what you mean in code.</p>\n\n<p>If you don't need the value-before-increment in your statement, don't use that form of the operator.  It's a minor issue, but unless you are working with a style guide that bans one\nversion in favor of the other altogether (aka a bone-headed style guide), you should use\nthe form that most exactly expresses what you are trying to do.</p>\n\n<p>QED, use the pre-increment version:</p>\n\n<pre><code>for (int i = 0; i != X; ++i) ...\n</code></pre>\n"}, {'answer_id': 9901848, 'author': 'Parag', 'author_id': 1297267, 'author_profile': 'https://Stackoverflow.com/users/1297267', 'pm_score': 8, 'selected': False, 'text': "<p><strong>i++</strong> is known as <em>post increment</em> whereas <em>++i</em> is called <em>pre increment.</em></p>\n<p><strong><code>i++</code></strong></p>\n<p><code>i++</code> is post increment because it increments <code>i</code>'s value by 1 after the operation is over.</p>\n<p>Let’s see the following example:</p>\n<pre><code>int i = 1, j;\nj = i++;\n</code></pre>\n<p>Here value of <code>j = 1</code>, but <code>i = 2</code>. Here the value of <code>i</code> will be assigned to <code>j</code> first, and then <code>i</code> will be incremented.</p>\n<p><strong><code>++i</code></strong></p>\n<p><code>++i</code> is pre increment because it increments <code>i</code>'s value by 1 before the operation.\nIt means <code>j = i;</code> will execute after <code>i++</code>.</p>\n<p>Let’s see the following example:</p>\n<pre><code>int i = 1, j;\nj = ++i;\n</code></pre>\n<p>Here the value of <code>j = 2</code> but <code>i = 2</code>. Here the value of <code>i</code> will be assigned to <code>j</code> after the <code>i</code> incremention of <code>i</code>.\nSimilarly, <code>++i</code> will be executed before <code>j=i;</code>.</p>\n<p>For your question  <strong>which should be used in the incrementation block of a for loop?</strong> the answer is, you can use any one... It doesn't matter. It will execute your <em>for</em> loop same number of times.</p>\n<pre><code>for(i=0; i&lt;5; i++)\n   printf(&quot;%d &quot;, i);\n</code></pre>\n<p>And</p>\n<pre><code>for(i=0; i&lt;5; ++i)\n   printf(&quot;%d &quot;, i);\n</code></pre>\n<p>Both the loops will produce the same output. I.e., <code>0 1 2 3 4</code>.</p>\n<p>It only matters where you are using it.</p>\n<pre><code>for(i = 0; i&lt;5;)\n    printf(&quot;%d &quot;, ++i);\n</code></pre>\n<p>In this case output will be <code>1 2 3 4 5</code>.</p>\n"}, {'answer_id': 14455004, 'author': 'munna', 'author_id': 1865289, 'author_profile': 'https://Stackoverflow.com/users/1865289', 'pm_score': -1, 'selected': False, 'text': '<p><em>a=i++</em> means <em>a</em> contains the current <em>i</em> value.</p>\n<p><em>a=++i</em> means <em>a</em> contains the incremented <em>i</em> value.</p>\n'}, {'answer_id': 17851935, 'author': 'Shivprasad Koirala', 'author_id': 993672, 'author_profile': 'https://Stackoverflow.com/users/993672', 'pm_score': 6, 'selected': False, 'text': '<p><code>i++</code>: In this scenario first the value is assigned and then increment happens. </p>\n\n<p><code>++i</code>: In this scenario first the increment is done and then value is assigned</p>\n\n<p>Below is the image visualization and also <a href="http://www.youtube.com/watch?v=lrtcfgbUXm4" rel="noreferrer">here is a nice practical video</a> which demonstrates the same.</p>\n\n<p><img src="https://i.stack.imgur.com/YTZO8.png" alt="enter image description here"></p>\n'}, {'answer_id': 18387007, 'author': 'GokhanAvci', 'author_id': 2551464, 'author_profile': 'https://Stackoverflow.com/users/2551464', 'pm_score': 3, 'selected': False, 'text': '<p>Shortly:</p>\n\n<p><code>++i</code> and <code>i++</code> works same if you are not writing them in a function. If you use something like <code>function(i++)</code> or <code>function(++i)</code> you can see the difference.</p>\n\n<p><code>function(++i)</code> says first increment i by 1, after that put this <code>i</code> into the function with new value.</p>\n\n<p><code>function(i++)</code> says put first <code>i</code> into the function after that increment <code>i</code> by 1.</p>\n\n<pre><code>int i=4;\nprintf("%d\\n",pow(++i,2));//it prints 25 and i is 5 now\ni=4;\nprintf("%d",pow(i++,2));//it prints 16 i is 5 now\n</code></pre>\n'}, {'answer_id': 18849765, 'author': 'Scitech', 'author_id': 2611926, 'author_profile': 'https://Stackoverflow.com/users/2611926', 'pm_score': 4, 'selected': False, 'text': '<p><code>++i</code>: is pre-increment the other is post-increment.</p>\n\n<p><code>i++</code>: gets the element and then increments it.<br>\n<code>++i</code>: increments i and then returns the element.</p>\n\n<p>Example:</p>\n\n<pre><code>int i = 0;\nprintf("i: %d\\n", i);\nprintf("i++: %d\\n", i++);\nprintf("++i: %d\\n", ++i);\n</code></pre>\n\n<p>Output:</p>\n\n<pre><code>i: 0\ni++: 0\n++i: 2\n</code></pre>\n'}, {'answer_id': 37202119, 'author': 'Anands23', 'author_id': 4643764, 'author_profile': 'https://Stackoverflow.com/users/4643764', 'pm_score': 4, 'selected': False, 'text': '<p><strong><code>++i</code> (Prefix operation): Increments and then assigns the value</strong><br>\n(eg): <code>int i = 5</code>, <code>int b = ++i</code>\n       In this case, 6 is assigned to b first and then increments to 7 and so on.</p>\n\n<p><strong><code>i++</code> (Postfix operation): Assigns and then increments the value</strong><br>\n(eg): <code>int i = 5</code>, <code>int b = i++</code>\n       In this case, 5 is assigned to b first and then increments to 6 and so on.</p>\n\n<p>Incase of for loop: <code>i++</code> is mostly used because, normally we use the starting value of <code>i</code> before incrementing in for loop. But depending on your program logic it may vary.</p>\n'}, {'answer_id': 37202251, 'author': 'Gopinath Kaliappan', 'author_id': 5444836, 'author_profile': 'https://Stackoverflow.com/users/5444836', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p><strong>The Main Difference is</strong> </p>\n  \n  <ul>\n  <li>i++ Post(<strong>After Increment</strong>) and </li>\n  <li><p>++i Pre  (<strong>Before Increment</strong>)</p>\n  \n  <ul>\n  <li>post if <code>i =1</code>  the loop increments like <code>1,2,3,4,n</code></li>\n  <li>pre  if <code>i =1</code>  the loop increments like <code>2,3,4,5,n</code></li>\n  </ul></li>\n  </ul>\n</blockquote>\n'}, {'answer_id': 46185399, 'author': 'IOstream', 'author_id': 6820948, 'author_profile': 'https://Stackoverflow.com/users/6820948', 'pm_score': 3, 'selected': False, 'text': "<p>The difference can be understood by this simple C++ code below:</p>\n\n<pre><code>int i, j, k, l;\ni = 1; //initialize int i with 1\nj = i+1; //add 1 with i and set that as the value of j. i is still 1\nk = i++; //k gets the current value of i, after that i is incremented. So here i is 2, but k is 1\nl = ++i; // i is incremented first and then returned. So the value of i is 3 and so does l.\ncout &lt;&lt; i &lt;&lt; ' ' &lt;&lt; j &lt;&lt; ' ' &lt;&lt; k &lt;&lt; ' '&lt;&lt; l &lt;&lt; endl;\nreturn 0;\n</code></pre>\n"}, {'answer_id': 46493511, 'author': 'Uddhav P. Gautam', 'author_id': 7232295, 'author_profile': 'https://Stackoverflow.com/users/7232295', 'pm_score': 3, 'selected': False, 'text': "<p><strong>Pre-crement means increment on the same line. Post-increment means increment after the line executes.</strong></p>\n<pre><code>int j = 0;\nSystem.out.println(j); // 0\nSystem.out.println(j++); // 0. post-increment. It means after this line executes j increments.\n\nint k = 0;\nSystem.out.println(k); // 0\nSystem.out.println(++k); // 1. pre increment. It means it increments first and then the line executes\n</code></pre>\n<p><strong>When it comes with OR, AND operators, it becomes more interesting.</strong></p>\n<pre><code>int m = 0;\nif((m == 0 || m++ == 0) &amp;&amp; (m++ == 1)) { // False\n    // In the OR condition, if the first line is already true\n    // then the compiler doesn't check the rest. It is a\n    // technique of compiler optimization\n    System.out.println(&quot;post-increment &quot; + m);\n}\n\nint n = 0;\nif((n == 0 || n++ == 0) &amp;&amp; (++n == 1)) { // True\n    System.out.println(&quot;pre-increment &quot; + n); // 1\n}\n</code></pre>\n<p><strong>In Array</strong></p>\n<pre><code>System.out.println(&quot;In Array&quot;);\nint[] a = { 55, 11, 15, 20, 25 };\nint ii, jj, kk = 1, mm;\nii = ++a[1]; // ii = 12. a[1] = a[1] + 1\nSystem.out.println(a[1]); // 12\n\njj = a[1]++; // 12\nSystem.out.println(a[1]); // a[1] = 13\n\nmm = a[1]; // 13\nSystem.out.printf(&quot;\\n%d %d %d\\n&quot;, ii, jj, mm); // 12, 12, 13\n\nfor (int val: a) {\n     System.out.print(&quot; &quot; + val); // 55, 13, 15, 20, 25\n}\n</code></pre>\n<p><strong>In C++ post/pre-increment of pointer variable</strong></p>\n<pre><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n\n    int x = 10;\n    int* p = &amp;x;\n\n    std::cout &lt;&lt; &quot;address = &quot; &lt;&lt; p &lt;&lt;&quot;\\n&quot;; // Prints the address of x\n    std::cout &lt;&lt; &quot;address = &quot; &lt;&lt; p &lt;&lt;&quot;\\n&quot;; // Prints (the address of x) + sizeof(int)\n    std::cout &lt;&lt; &quot;address = &quot; &lt;&lt; &amp;x &lt;&lt;&quot;\\n&quot;; // Prints the address of x\n\n    std::cout &lt;&lt; &quot;address = &quot; &lt;&lt; ++&amp;x &lt;&lt; &quot;\\n&quot;; // Error. The reference can't reassign, because it is fixed (immutable).\n}\n</code></pre>\n"}, {'answer_id': 46514153, 'author': 'Nihal Reddy', 'author_id': 7091562, 'author_profile': 'https://Stackoverflow.com/users/7091562', 'pm_score': 3, 'selected': False, 'text': '<p>The following C code fragment illustrates the difference between the pre and post increment and decrement operators:</p>\n\n<pre><code>int  i;\nint  j;\n</code></pre>\n\n<p>Increment operators:</p>\n\n<pre><code>i = 1;\nj = ++i;    // i is now 2, j is also 2\nj = i++;    // i is now 3, j is 2\n</code></pre>\n'}, {'answer_id': 48873975, 'author': 'carloswm85', 'author_id': 7389293, 'author_profile': 'https://Stackoverflow.com/users/7389293', 'pm_score': 3, 'selected': False, 'text': "<h1><strong>i++ and ++i</strong></h1>\n<p>This little code may help to visualize the difference from a different angle than the already posted answers:</p>\n<pre><code>int i = 10, j = 10;\n  \nprintf (&quot;i is %i \\n&quot;, i);\nprintf (&quot;i++ is %i \\n&quot;, i++);\nprintf (&quot;i is %i \\n\\n&quot;, i);\n  \nprintf (&quot;j is %i \\n&quot;, j);\nprintf (&quot;++j is %i \\n&quot;, ++j);\nprintf (&quot;j is %i \\n&quot;, j);\n</code></pre>\n<p>The outcome is:</p>\n<pre><code>//Remember that the values are i = 10, and j = 10\n\ni is 10 \ni++ is 10     //Assigns (print out), then increments\ni is 11 \n\nj is 10 \n++j is 11    //Increments, then assigns (print out)\nj is 11 \n</code></pre>\n<p>Pay attention to the before and after situations.</p>\n<h1><strong>for loop</strong></h1>\n<p>As for which one of them should be used in an incrementation block of a for loop, I think that the best we can do to make a decision is use a good example:</p>\n<pre><code>int i, j;\n\nfor (i = 0; i &lt;= 3; i++)\n    printf (&quot; &gt; iteration #%i&quot;, i);\n\nprintf (&quot;\\n&quot;);\n\nfor (j = 0; j &lt;= 3; ++j)\n    printf (&quot; &gt; iteration #%i&quot;, j);\n</code></pre>\n<p>The outcome is:</p>\n<pre><code>&gt; iteration #0 &gt; iteration #1 &gt; iteration #2 &gt; iteration #3\n&gt; iteration #0 &gt; iteration #1 &gt; iteration #2 &gt; iteration #3 \n</code></pre>\n<p>I don't know about you, but I don't see any difference in its usage, at least in a for loop.</p>\n"}, {'answer_id': 51704394, 'author': 'Jeet Parikh', 'author_id': 9906531, 'author_profile': 'https://Stackoverflow.com/users/9906531', 'pm_score': 1, 'selected': False, 'text': '<p>You can think of the internal conversion of that as <strong>multiple statements</strong>:</p>\n<pre><code>// case 1\n\ni++;\n\n/* you can think as,\n * i;\n * i= i+1;\n */\n\n\n\n// case 2\n\n++i;\n\n/* you can think as,\n * i = i+i;\n * i;\n */\n</code></pre>\n'}, {'answer_id': 54911582, 'author': 'Francesco Boi', 'author_id': 1714692, 'author_profile': 'https://Stackoverflow.com/users/1714692', 'pm_score': 5, 'selected': False, 'text': '<p>The only difference is the order of operations between the increment of the variable and the value the operator returns.</p>\n<p>This code and its output explains the the difference:</p>\n<pre><code>#include&lt;stdio.h&gt;\n\nint main(int argc, char* argv[])\n{\n  unsigned int i=0, a;\n  printf(&quot;i initial value: %d; &quot;, i);\n  a = i++;\n  printf(&quot;value returned by i++: %d, i after: %d\\n&quot;, a, i);\n  i=0;\n  printf(&quot;i initial value: %d; &quot;, i);\n  a = ++i;\n  printf(&quot; value returned by ++i: %d, i after: %d\\n&quot;,a, i);\n}\n</code></pre>\n<p>The output is:</p>\n<pre><code>i initial value: 0; value returned by i++: 0, i after: 1\ni initial value: 0;  value returned by ++i: 1, i after: 1\n</code></pre>\n<p>So basically <code>++i</code> returns the value after it is incremented, while <code>i++</code> return the value before it is incremented. At the end, in both cases the <code>i</code> will have its value incremented.</p>\n<p>Another example:</p>\n<pre><code>#include&lt;stdio.h&gt;\n\nint main ()\n  int i=0;\n  int a = i++*2;\n  printf(&quot;i=0, i++*2=%d\\n&quot;, a);\n  i=0;\n  a = ++i * 2;\n  printf(&quot;i=0, ++i*2=%d\\n&quot;, a);\n  i=0;\n  a = (++i) * 2;\n  printf(&quot;i=0, (++i)*2=%d\\n&quot;, a);\n  i=0;\n  a = (i++) * 2;\n  printf(&quot;i=0, (i++)*2=%d\\n&quot;, a);\n  return 0;\n}\n</code></pre>\n<p>Output:</p>\n<pre><code>i=0, i++*2=0\ni=0, ++i*2=2\ni=0, (++i)*2=2\ni=0, (i++)*2=0\n</code></pre>\n<hr />\n<h2>Many times there is no difference</h2>\n<p>Differences are clear when the returned value is assigned to another variable or when the increment is performed in concatenation with other operations where operations precedence is applied (<code>i++*2</code> is different from <code>++i*2</code>, as well as <code>(i++)*2</code> and <code>(++i)*2</code>) in many cases they are interchangeable. A classical example is the for loop syntax:</p>\n<pre><code>for(int i=0; i&lt;10; i++)\n</code></pre>\n<p>has the same effect of</p>\n<pre><code>for(int i=0; i&lt;10; ++i)\n</code></pre>\n<h2>Efficiency</h2>\n<p>Pre-increment is always at least as efficient as post-increment: in fact post-increment usually involves keeping a copy of the previous value around and might add a little extra code.</p>\n<p>As others have suggested, due to compiler optimisations many times they are equally efficient, probably a for loop lies within these cases.</p>\n<h2>Rule to remember</h2>\n<p>To not make any confusion between the two operators I adopted this rule:</p>\n<p><strong>Associate the position of the operator <code>++</code> with respect to the variable <code>i</code> to the order of the <code>++</code> operation with respect to the assignment</strong></p>\n<p>Said in other words:</p>\n<ul>\n<li><code>++</code> <strong>before</strong> <code>i</code> means <strong>incrementation</strong> must be carried out <strong>before</strong> <strong>assignment</strong>;</li>\n<li><code>++</code> <strong>after</strong> <code>i</code> means <strong>incrementation</strong> must be carried out <strong>after</strong> <strong>assignment</strong>:</li>\n</ul>\n'}, {'answer_id': 70671642, 'author': 'Talat El Beick', 'author_id': 13562196, 'author_profile': 'https://Stackoverflow.com/users/13562196', 'pm_score': 3, 'selected': False, 'text': '<p>In simple words the difference between both is in the steps take a look to the image below.</p>\n<p><a href="https://i.stack.imgur.com/CA8tL.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/CA8tL.jpg" alt="enter image description here" /></a></p>\n<p>Example:</p>\n<pre class="lang-c prettyprint-override"><code>int i = 1;\nint j = i++;\n</code></pre>\n<p>The <code>j</code> result is <code>1</code></p>\n<pre class="lang-c prettyprint-override"><code>int i = 1;\nint j = ++i;\n</code></pre>\n<p>The <code>j</code> result is <code>2</code></p>\n<p><strong>Note:</strong> in both cases <code>i</code> values is <code>2</code></p>\n'}]
['https://Stackoverflow.com/questions/24853', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2128/']