{"id":7814,"date":"2016-04-25T10:02:46","date_gmt":"2016-04-25T09:02:46","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=7814"},"modified":"2026-03-26T16:48:41","modified_gmt":"2026-03-26T20:48:41","slug":"initialization-of-variables-in-embedded-applications","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2016\/04\/25\/initialization-of-variables-in-embedded-applications\/","title":{"rendered":"Initialization of variables in embedded applications"},"content":{"rendered":"<p>There is a variety of reasons why C is so widely favored as a programming language for embedded software. It is a powerful, expressive and efficient language &#8211; these are certainly attractions. But another factor is also important: availability of expertise. C is used very widely outside of embedded, so many programmers know the language. Staffing up an embedded software project should, therefore, be quite straightforward.<\/p>\n<p>However, programming for embedded systems, even if it is in a familiar language, is still different from programming a desktop computer &#8230;<!--more--><\/p>\n<p>There are numerous aspects of the use of C that differ in an embedded context. An interesting example is the initialization of variables.<\/p>\n<p>Care should always be taken to ensure that a variable has a sensible initial value. Even if a language specifies that all variables are initially <strong>0<\/strong>, it is bad programming style to not show an explicit initialization. Consider this code:<\/p>\n<pre>void my_fun()\n{\n int auto_variable = 101;\n ...\n\n<\/pre>\n<p>When you declare a variable, you have the option to give it an initial vale. This seems a good idea, but there is a tiny downside. As this variable is automatic, it comes into existence when the function is entered, being allocated space on the stack or in a register. The initialization code is then executed to load the stack location or register with the required value. I believe that it is better to specify this more explicitly, thus:<\/p>\n<pre>void my_fun()\n{\n int auto_variable;<\/pre>\n<pre> auto_variable = 101;\n ...\n\n<\/pre>\n<p>This makes no difference at all to the executable code. My motivation in suggesting the change is making the code clearer for someone reading\/maintaining it later.<\/p>\n<p>So, what about <strong>static<\/strong> variables? They should be simpler, as their space is allocated at compile time. Thus, code like this is common:<\/p>\n<pre>void my_fun()\n{\n static int static_variable = 202;\n ...\n\n<\/pre>\n<p>In a desktop computer program, this makes sense. When the program is run, the code and initialized data is read from disk into memory and execution started. However, most embedded systems do not work like that. There is likely to be a copy mechanism that sets up all the initialized variables in RAM from data in Flash. Again, it is my view that this process should be shown more explicitly:<\/p>\n<pre>void my_fun()\n{\n static int static_variable;<\/pre>\n<pre> static_variable = 202;\n ...\n\n<\/pre>\n<p>This latter example would also apply to variables that are declared outside of functions, which are intrinsically static. Their initialization should be at the site of first use.<\/p>\n<p>Ultimately, in the case of a few C <strong>int<\/strong> variables, I am just being picky. But I strongly value clarity in programming style. Always think about the guy who needs to understand\/modify\/maintain you code sometime in the future. It might even be you!<\/p>\n<p>If we go beyond C and use C++, these same issues occur, but can be much more serious. The initialization of a C++ object is likely to be much more complex that a single assignment.<\/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>There is a variety of reasons why C is so widely favored as a programming language for embedded software. It&#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,339,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-7814","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-development-tools","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7814","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=7814"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7814\/revisions"}],"predecessor-version":[{"id":10465,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7814\/revisions\/10465"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=7814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=7814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=7814"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=7814"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=7814"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=7814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}