{"id":6507,"date":"2024-08-03T00:01:17","date_gmt":"2024-08-03T07:01:17","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6507"},"modified":"2024-07-27T11:21:36","modified_gmt":"2024-07-27T18:21:36","slug":"what-is-this-why-do-i-want-it-what-does-it-do","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6507","title":{"rendered":"What is This? Why do I Want It? What Does it Do?"},"content":{"rendered":"<p>Sometimes I turn off my programmer brain and look at code to admire it in an innocent way. At first glance a C program source code file looks poetic, using the same patterns and flow. Code is also cryptic, which inspires many programmers to try to invent a new way to do something in a charming and confusing manner. One of my attempts was to rationalize this expression: <code>++a++<\/code><br \/>\n<!--more--><br \/>\nAt first, I&#8217;m delighted with the symmetry of <code>++a++<\/code>. It&#8217;s pretty cool. But what would it do? How would the <code>++<\/code> (increment) operator bind to the variable?<\/p>\n<p>The order of operations for <code>++<\/code> is right to left. So in my frontal lobe, I figured that <code>a++<\/code> would happen first, adding one to the value stored in variable <code>a<\/code>. Then this value would be incremented again. Neat but improper, as the compiler informed me when building this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_08_03-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_08_03-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a = -1;\r\n\r\n    printf(\"%d\\n\",++a++);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<p><code>2024_08_03-Lesson.c:7:16: error: expression is not assignable<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(\"%d\\n\",++a++);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ~~~<br \/>\n1 error generated.<\/code><\/p>\n<p>Without knowing the specifics, my guess is that the compiler performs the <code>a++<\/code> operation first. It translates the expression into the value of variable <code>a<\/code> plus one, which would be zero. Then the code reads the expression as <code>++0<\/code>, which is the &#8220;non-assignable&#8221; part: You cannot increment a result.<\/p>\n<p>Or it could work the other way with <code>++a<\/code> happening first, which again would be zero, and then trying to increment zero. Either way, the expression &mdash; which still looks cool &mdash; is invalid in C.<\/p>\n<p>Using parentheses doesn&#8217;t help: <code>(++a)++<\/code> or <code>++(a++)<\/code> yields the same error. These repeat the same illogic of incrementing a result.<\/p>\n<p>The resolution is to figure out what the code is wanting to do. If you replace the bogus expression with <code>(++a)+1<\/code>, negative one is incremented to zero, then one is added. The result is one. The same result appears if you code it as <code>1+(a++)<\/code>, which can also be written without parentheses: <code>1+a++<\/code>. This is the original intent of the <code>++a++<\/code> expression, so either way works.<\/p>\n<p>I shall continue to strive to locate interesting and pretty things in C programming to see how they work &mdash; or not. Sometimes programming has more to do with appearance than practicality, which is probably the motivation behind the entire obfuscated C movement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C has some marvelous operators that you can scrunch together &#038;mdash or not. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6507\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6507","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6507"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6507\/revisions"}],"predecessor-version":[{"id":6522,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6507\/revisions\/6522"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}