{"id":962,"date":"2014-10-04T00:01:54","date_gmt":"2014-10-04T07:01:54","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=962"},"modified":"2014-09-27T08:19:25","modified_gmt":"2014-09-27T15:19:25","slug":"name-your-variables","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=962","title":{"rendered":"Name Your Variables"},"content":{"rendered":"<p>As you sit and madly pound out code, variable names are probably not at the top of your list of things to do better. I tend to use <code>x<\/code> for my loops, <code>c<\/code> or <code>ch<\/code> for characters, and other variables, including <code>a<\/code>, <code>b<\/code>, and <code>c<\/code>. This is an acceptable approach, but for larger programs such variable names not doing you any favors.<br \/>\n<!--more--><br \/>\nVariable names can be descriptive. They could tell you something about the value they represent, which adds readability to your code. For example:<\/p>\n<p><code>s = d\/t;<\/code><\/p>\n<p>Obviously the above statement is some sort of equation, which you can understand if you&#8217;ve been studying C for longer than a day. The following example works better, thanks to more descriptive variable names:<\/p>\n<p><code>speed = distance\/time;<\/code><\/p>\n<p>I&#8217;m also a fan of something called <em>medial capitalization<\/em> for my variable names. This is technique is also known as <em>camel case<\/em>, simply for the &#8220;humps&#8221; it puts in the names:<\/p>\n<p><code>int retryAttempts, valueOverTime, initialEggCondition;<\/code><\/p>\n<p>Because spaces aren&#8217;t allowed in variable names, many programmers opt to use an underscore to separate &#8220;words&#8221; in a variable name:<\/p>\n<p><code>int retry_attempts, value_over_time, initial_egg_condition;<\/code><\/p>\n<p>I admit that this technique does make the variable names easier to read, but it&#8217;s not as easy to type.<\/p>\n<blockquote><p>You can&#8217;t use a hyphen (the <code>-<\/code> character) in a C variable name as it&#8217;s interpreted as the subtraction operator.<\/p><\/blockquote>\n<p>Microsoft uses <em>Hungarian Notation<\/em> for their variable names. The idea is to identify the type of variable by looking at its name. For example, the variable name <code>szAddress<\/code> is a null-terminated string, <code>lHeight<\/code> would be a <em>long int<\/em> variable, and so on.<\/p>\n<p>You can read more about Hungarian Notation on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hungarian_notation\" target=\"_blank\">Wikipedia<\/a>, which offers some examples.<\/p>\n<p>The C language lacks any specific rules on variable names, although one limitation is common: Internal variables are named with a double underscore prefix. If you&#8217;ve ever spelunked in the header files, then you&#8217;ve probably seen variables like <code>__secret<\/code>. The double underscore identifies these internal variables, flagging them as special.<\/p>\n<p>It&#8217;s recommended that you don&#8217;t begin a variable name in your C code with an underscore or double underscore prefix and to avoid double underscores within a variable name. Aside from that, start the variable name with a letter, then use upper case, lower case, underscores, and even numbers if you like.<\/p>\n<p>My advice is to keep the variable names descriptive. That&#8217;s always a plus.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Beyond short and descriptive, you&#8217;ll find various variable naming conventions at your disposal. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=962\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-962","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\/962","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=962"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":1014,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/962\/revisions\/1014"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}