{"id":1281,"date":"2022-08-30T15:15:13","date_gmt":"2022-08-30T15:15:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/why-cant-i-do-i-in-c-like-languages-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:13","modified_gmt":"2022-08-30T15:15:13","slug":"why-cant-i-do-i-in-c-like-languages-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-cant-i-do-i-in-c-like-languages-collection-of-common-programming-errors\/","title":{"rendered":"Why can&#39;t I do ++i++ in C-like languages?-Collection of common programming errors"},"content":{"rendered":"<p>Though the short answer &#8220;it&#8217;s not an lvalue&#8221; is <em>correct<\/em>, that&#8217;s perhaps just begging the question. <em>Why<\/em> isn&#8217;t it an <em>lvalue<\/em>? Or, as we say in C#, a <em>variable<\/em>.<\/p>\n<p>The reason is because <em>you cannot have your cake and eat it too<\/em>. Work it out logically:<\/p>\n<p>First off, the meaning of a ++ operator in C#, whether postfix or prefix, is <strong>&#8220;take the value of this variable, increment the value, assign the new value to the variable, and produce a value as a result&#8221;<\/strong>. The value produced as the result is either the original value or the incremented value, depending on whether it was a postfix or a prefix. But either way, you produce a value.<\/p>\n<p>Second, <strong>the value of a variable is always the current contents of that variable<\/strong>. (Modulo certain bizarre threading scenarios that would take us far afield.)<\/p>\n<p>I hope you agree that these are perfectly sensible rules.<\/p>\n<p>Now it should be clear why the result of i++ cannot be a variable, but in case it isn&#8217;t, let me make it clear:<\/p>\n<p>Suppose i is 10. The meaning of i++ should be &#8220;get the value of i \u2014 10 \u2014 increment it \u2014 11 \u2014 store it \u2014 i is now 11 \u2014 and give the original value as the result \u2014 10&#8221;. So when you say print(i++) it should print 10, and 11 should be stored in i.<\/p>\n<p>Now suppose the meaning of i++ is to return the <em>variable<\/em>, not the <em>value<\/em>. You say print(i++) and what happens? You get the value of i \u2014 10 \u2014 increment it \u2014 11 \u2014 store it \u2014 i is now 11 \u2014 and give the variable back as a result. What&#8217;s the current value of the variable? 11! Which is exactly what you DON&#8217;T want to print.<\/p>\n<p>In short, if i++ returned a variable then it would be doing <strong>exactly the opposite<\/strong> of the intended meaning of the operator! Your proposal is logically inconsistent, which is why no language does it that way.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:14:14. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Though the short answer &#8220;it&#8217;s not an lvalue&#8221; is correct, that&#8217;s perhaps just begging the question. Why isn&#8217;t it an lvalue? Or, as we say in C#, a variable. The reason is because you cannot have your cake and eat it too. Work it out logically: First off, the meaning of a ++ operator in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1281","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1281"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1281\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}