{"id":358,"date":"2009-08-10T10:19:00","date_gmt":"2009-08-10T09:19:00","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=358"},"modified":"2026-03-26T16:30:46","modified_gmt":"2026-03-26T20:30:46","slug":"i-or-i","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2009\/08\/10\/i-or-i\/","title":{"rendered":"++i or i++?"},"content":{"rendered":"<p>If you are reading this blog, you are probably knowledgeable about embedded software and, therefore, like me, you consider yourself proficient in C. C is still the dominant language for embedded work. I read an <a href=\"http:\/\/www.embedded.com\/design\/218600142;jsessionid=BTLSLUB1WUMXXQE1GHRSKH0CJUNN2JVN?printable=true\" target=\"_blank\" rel=\"noopener noreferrer\">article<\/a> recently which espoused the view that &#8220;real men program in C&#8221; &#8211; I rather liked that. It also suggested that, since relatively few new graduates know C, there is an increasing shortage of expertise. This is great news for us old hands.<\/p>\n<p>However, we should not be complacent. I think that I know C quite well. After all, it is quite a small language, so getting a grip should not be hard. Even aspects of it that are specifically interesting to embedded developers [and there are quite a few] should not be too difficult to master. But sometimes it is possible to get caught out &#8230;<\/p>\n<p><!--more-->I will pose a question: what is the difference between <strong>++i<\/strong> and <strong>i++<\/strong>?<br \/>\nI am sure that you know that both expressions result in the variable <strong>i<\/strong> being incremented, but the first expression yields the incremented value if <strong>i<\/strong>, whereas the second returns the value prior to incrementing. OK so far. But what if the expression result is discarded in a situation like this, for example:<\/p>\n<p><strong>for (i=0; i&lt;5; i++)<\/strong><\/p>\n<p>Does it make any difference which form of the increment operator is used in this context? I would have said no. But then I had a conversation with one of our compiler team and now I know better.<\/p>\n<p>Functionally, it makes no difference &#8211; <strong>++i<\/strong> and <strong>i++<\/strong> would both do the right thing. However, embedded developers are interested in more than just functionality &#8211; we are always interested in overheads. Think about what code might be compiled for each form of the expression. For <strong>++i<\/strong>, the variable is simply incremented; for <strong>i++<\/strong>, the value needs to be stored somewhere and then the variable is incremented. This means that the post increment form needs some additional storage, which is an unwanted overhead.<\/p>\n<p>Of course, for a simple integer, the overhead is quite small. But, if the variable were actually a complex C++ object, it might be a more serious matter.<\/p>\n<p>It can be argued that a good compiler would observe that the expression result is not needed and this code would be optimized away. I sincerely hope that this would be the case. However, it is sloppy programming to just rely on compiler optimization to address the ignorance or laziness of the programmer.<\/p>\n<p>If you have any other examples of subtle C features that are interesting to embedded developers, please <a href=\"mailto:colin_walls@mentor.com\">email<\/a> me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are reading this blog, you are probably knowledgeable about embedded software and, therefore, like me, you consider yourself&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,327],"industry":[],"product":[],"coauthors":[],"class_list":["post-358","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-optimization"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=358"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"predecessor-version":[{"id":9778,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/358\/revisions\/9778"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=358"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=358"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=358"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}