{"id":8689,"date":"2018-05-14T09:47:27","date_gmt":"2018-05-14T08:47:27","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=8689"},"modified":"2026-03-26T16:54:28","modified_gmt":"2026-03-26T20:54:28","slug":"4-handy-embedded-software-tips","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2018\/05\/14\/4-handy-embedded-software-tips\/","title":{"rendered":"4 handy embedded software tips"},"content":{"rendered":"<p>From time to time, I like to muse on some useful programming tips for embedded developers. This is such a time. I am not presenting hard and fast rules; I am just suggesting some possible good practices. I am also keen to promote discussion, as an alternate point of view is always interesting. Do contact me by comment or via <a href=\"mailto:colin_walls@mentor.com\">email<\/a> or social media &#8230;<!--more--><\/p>\n<h3>Always use ++i instead of i++ if you are not saving the result of the expression<\/h3>\n<p>Every C programmer knows the difference between the prefix and postfix form of the ++ operator: ++i increments i and returns the new value; i++ increments i and returns the old value. They are both useful in various contexts. However, if you are not using the returned value &#8211; i.e. you just convert the the expression into a statement &#8211; does it matter which one you use? For example, you might write:<\/p>\n<pre><strong>for (i=0; i&lt;10; i++)<\/strong>\n<strong>   ...<\/strong>\n\n<strong>or<\/strong>\n\n<strong>for (i=0; i&lt;10; ++i)<\/strong>\n<strong>   ...<\/strong>\n\n<\/pre>\n<p>They both do exactly the same thing, but is one better than the other?<\/p>\n<p>The answer is: most of the time it makes no difference which form you use. If i is an int the resulting code would be identical. However, if i were an instantiation of a user-written class, and the ++ operator(s) had be overloaded, it would be a different story. The postfix form would probably result in some extra memory being used to accommodate the old value ready to return. Although compiler optimization might save the day, it is good practice to use the prefix form by default.<\/p>\n<h3>Never test for equality of floating point values<\/h3>\n<p>When floating point calculations are performed, there is always the possibility of tiny rounding errors. This results in code like this:<\/p>\n<pre><strong>float x, y;<\/strong>\n<strong>...<\/strong>\n<strong>if (x == y)<\/strong>\n<strong>   ...<\/strong><\/pre>\n<pre><\/pre>\n<p>being misleading. The result of some calculations may be that x and y are both 6.0. However, their actual values might be 6.0000001 and 6.0000002 respectively. So, the equality comparison would produce the wrong result. Clearly a comparison for less than or greater than would also need care.<\/p>\n<h3>Only have a single return point from a function; it makes for clear code<\/h3>\n<p>This tip is somewhat controversial and, to be honest, I could swing either way. The idea is that, there is only one entry point to a function: the top. There should, therefore, be a single exit\/return point: the bottom. The thinking is that this makes the structure clear and avoids the possibility that any function exit code is overlooked.<\/p>\n<p>The opposing argument is that, to achieve this structure, in some cases, may require some quite convoluted logic to steer execution to the bottom. The best approach is to perform parameter verification etc., which might result in an early exit, at the top and include one or more return statements there, if necessary. Then, enter the main body of the function, where there is a single exit at the bottom.<\/p>\n<p>Coding standards like MISRA C encourage the single exit approach and it is mandated by some safety standards like IEC 61508 and ISO 26262.<\/p>\n<h3>Always initialize a variable at the point of definition. Never assume that it is zero<\/h3>\n<p>Even though the language standard says that static variables in C\/C++ will always be initialized to zero, it is bad programming practice to assume that this is the case. If nothing else, readability of the code is improved by an explicit initialization. I feel that this is best done when the variable is declared, but it might be argued that the declaration are initialization are [for automatic variables anyway] separate things and that should be illustrated. So, looking at this code:<\/p>\n<pre><strong>void fun()<\/strong>\n<strong>{<\/strong>\n<strong>   int alpha, beta=2;<\/strong>\n\n<strong>   alpha = 3;<\/strong>\n<strong>   ...<\/strong>\n\n<\/pre>\n<p>Which variable has been handled in the best\/clearest fashion? The generated code will be the same either way.<\/p>\n<p><a href=\"http:\/\/www.linkedin.com\/in\/colinwalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6579\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/linkedin.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/twitter.com\/colin_walls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6583\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/twitter.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/www.facebook.com\/colinwalls.author\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6591\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/facebook.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/plus.google.com\/116301748426290440139\/posts?hl=en%3Fhl=en\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6587\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/google.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"http:\/\/www.slideshare.net\/ColinWalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6595\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/slideshare.jpg\" alt=\"\" width=\"41\" height=\"41\" \/><\/a><a href=\"http:\/\/blogs.mentor.com\/colinwalls\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6599\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/wordpress.jpg\" alt=\"\" width=\"44\" height=\"44\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From time to time, I like to muse on some useful programming tips for embedded developers. This is such a&#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":[339,300,484,340,478],"industry":[],"product":[],"coauthors":[],"class_list":["post-8689","post","type-post","status-publish","format-standard","hentry","category-news","tag-development-tools","tag-embedded-software","tag-floating-point","tag-programming-languages","tag-sourcery-codebench"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/8689","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=8689"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/8689\/revisions"}],"predecessor-version":[{"id":10687,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/8689\/revisions\/10687"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=8689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=8689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=8689"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=8689"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=8689"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=8689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}