{"id":7948,"date":"2016-08-08T10:44:38","date_gmt":"2016-08-08T09:44:38","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=7948"},"modified":"2026-03-26T16:49:28","modified_gmt":"2026-03-26T20:49:28","slug":"variable-declarations-in-c-plenty-of-pitfalls","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2016\/08\/08\/variable-declarations-in-c-plenty-of-pitfalls\/","title":{"rendered":"Variable declarations in C &#8211; plenty of pitfalls"},"content":{"rendered":"<p>It is widely felt that C is a very good language for embedded applications, as it is expressive, compact and powerful. It is possible to write very clear, readable code, but that takes care. Even the simplest things, like the declaration of variables, have pitfalls for the unwary &#8230;<!--more--><\/p>\n<p>In C, you can declare variables in one of two places: the head of a block or outside of function code. In either case, the syntax is the same:<\/p>\n<pre><strong>[ &lt;qualifier&gt; ] &lt;type&gt; &lt;identifier&gt; [, &lt;identifier&gt;] ;\n\n<\/strong><\/pre>\n<p>For example:<\/p>\n<pre><strong>float x, y, z;\n\n<\/strong><\/pre>\n<p>Many developers feel that it is bad practice to declare for than one variable on each line. For example:<\/p>\n<pre><strong>int temperature, time_delay;   \/\/ user selected parameters\n\n<\/strong><\/pre>\n<p>is not such a good idea, as it might be written more clearly thus:<\/p>\n<pre><strong>int temperature;   \/\/ user selected temperature\nint time_delay;    \/\/ user selected interval\n\n<\/strong><\/pre>\n<p>It may be acceptable to declare multiple variables on one line, if they are closely bound together. For example:<\/p>\n<pre><strong>static float x, y, z; \/\/ spacial coordinates\n\n<\/strong><\/pre>\n<p>However, it might be argued that this is a case when a data structure &#8211; <strong>struct<\/strong> or <strong>class<\/strong> &#8211; might be better.<\/p>\n<p>A further complication is that a declaration can contain variables of different types &#8211; base types and pointers, thus:<\/p>\n<pre><strong>int n, *p, **p2p;\n\n<\/strong><\/pre>\n<p>This is clear enough &#8211; we have an <strong>int<\/strong>, a pointer to <strong>int<\/strong> and a pointer to a pointer to <strong>int<\/strong>. But there is still the commenting issue.<\/p>\n<p>Matters are made worse if we write a declaration like this:<\/p>\n<pre><strong>int* p;\n\n<\/strong><\/pre>\n<p>This is favored in the C++ world and is arguably clearer &#8211; the type is pointer to <strong>int<\/strong>. However, this code:<\/p>\n<pre><strong>int* p1, p2;\n\n<\/strong><\/pre>\n<p>is misleading. Although <strong>p1<\/strong> is a pointer to int, <strong>p2<\/strong> is just a plain <strong>int<\/strong>.<\/p>\n<p>So, what is the answer? Multiple variables on the line or not? In my view, a single variable per line should be the default, reserving multiple declarations for special cases. If you have views on this matter, I would welcome a comment, <a href=\"mailto:colin_walls@mentor.com\">email<\/a> or contact via social media.<\/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>It is widely felt that C is a very good language for embedded applications, as it is expressive, compact and&#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,478],"industry":[],"product":[],"coauthors":[],"class_list":["post-7948","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-development-tools","tag-embedded-software","tag-programming-languages","tag-sourcery-codebench"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7948","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=7948"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7948\/revisions"}],"predecessor-version":[{"id":10496,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7948\/revisions\/10496"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=7948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=7948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=7948"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=7948"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=7948"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=7948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}