{"id":1596,"date":"2015-10-17T00:01:19","date_gmt":"2015-10-17T07:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1596"},"modified":"2015-10-10T09:08:59","modified_gmt":"2015-10-10T16:08:59","slug":"on-the-fly-variables","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1596","title":{"rendered":"On-the-fly Variables"},"content":{"rendered":"<p>Traditionally, a C program announces its variables at the start of a function block. The variables are presented by type and name, and they can be initialized at that time as well. This tradition isn&#8217;t a rule, and many C programmers break it.<br \/>\n<!--more--><br \/>\nI&#8217;m not one of those programmers. Being a traditionalist, and also desiring some consistency, I keep variable declarations at the start of my code. My reasons are the same as my motivation for keeping my car keys in the same spot in my house; I can always find them &mdash; providing that I habitually keep them at that location. (That process occasionally has flaws, but you get my point.)<\/p>\n<p>One place I&#8217;ve seen variables declared in a non-traditional manner is in a <em>for<\/em> loop. The counting variable, which is usually a throw-away integer, is declared right in the <em>for<\/em> statement. Inhale the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    for(int x = 0;x&lt;10;x++)\r\n        printf(\"Counting: %d\\n\",x+1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>x<\/code> is declared right at the start of the <em>for<\/em> statement: <code>int x = 0<\/code>. I see this technique used often, and it&#8217;s okay: The code compiles and runs just as if you declared <code>x<\/code> on a line by itself.<\/p>\n<p>In fact, even if you used the identical technique in another <em>for<\/em> loop elsewhere in the code, the compiler won&#8217;t balk. That&#8217;s a credit to habit, but I&#8217;m still more comfortable declaring all the code&#8217;s variables in one batch.<\/p>\n<p>Yes, my insistence upon tradition means that you must be flexible with your text editor. Many times I&#8217;ve been deep in the bowels of some 500-line monster code and suddenly needed a new variable. So I fly up to the top of the block and write the variable&#8217;s definition, then fly back. I&#8217;m okay with that process.<\/p>\n<p>And remember that you can&#8217;t use a variable before it&#8217;s declared. C programs read top-down, so the variable must be declared &#8220;above&#8221; wherever it appears in the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Where to declare variables in your code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1596\">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-1596","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\/1596","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=1596"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1596\/revisions"}],"predecessor-version":[{"id":1606,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1596\/revisions\/1606"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}