{"id":1928,"date":"2016-05-21T00:01:30","date_gmt":"2016-05-21T07:01:30","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1928"},"modified":"2016-05-14T09:14:49","modified_gmt":"2016-05-14T16:14:49","slug":"macros-with-variables","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1928","title":{"rendered":"Macros with Variables"},"content":{"rendered":"<p>Some C coders really go nuts with macros. I&#8217;m not a big fan of those people.<br \/>\n<!--more--><br \/>\nThe C language is obscure enough. When you add to that obscurity the use of macros, it makes a difficult language even more incomprehensible. Yet, some coders revel in that obfuscation.<\/p>\n<p>A few <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=986\">Lessons back<\/a>, someone asked me to describe how a loop works. I committed the terrible sin of actually explaining how a <em>for<\/em> loop did its job. They just wanted to know, &#8220;Okay, so I need to repeat a chunk of code ten times.&#8221; That&#8217;s the kind of statement you get from a student who is required to take a programming course but doesn&#8217;t give a damn about learning anything.<\/p>\n<p>Anyway, the C language lacks a direct looping keyword, but you can define one as a macro, which I did in this code example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define repeat( c ) for(int c1=0;c1&lt;c;c1++)\r\n\r\nint main()\r\n{\r\n    repeat(10)\r\n    {\r\n        puts(\"Hello!\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>#define<\/code> preprocessor directive at Line 3 sets the <em>repeat()<\/em> macro equal to a standard <em>for<\/em> loop. Variable <code>c<\/code> from the <em>repeat<\/em> macro is placed into the <em>for<\/em> loop at the proper locations to make a simple loop. Another variable, integer <code>c1<\/code>, is created inside the <em>for<\/em> statement with <code>int c1=0<\/code>. That&#8217;s perfectly legal, providing that the variable isn&#8217;t declared elsewhere in the code.<\/p>\n<p>The end result of the macro is seen in the <em>main()<\/em> function: The <em>repeat()<\/em> &#8220;function&#8221; processes a series of statements a given number of times. This kind of trick wouldn&#8217;t work with a true function; only a macro can set up such a statement within your code.<\/p>\n<p>Another way to define the macro would be as follows:<\/p>\n<pre><code>#define repeat( c ) int c1 ; for(c1=0;c1&lt;c;c1++)<\/code><\/pre>\n<p>Above, the <em>repeat<\/em> macro is expanded into two statements. The first declares the <em>int<\/em> variable <code>c1<\/code>. The second is the <em>for<\/em> loop that uses variable <code>c1<\/code> as well as variable <code>c<\/code>. The program output is the same. My point is to demonstrate that a <code>#define<\/code> macro can include more than a single statement. And that&#8217;s where things can get even more confusing.<\/p>\n<p>My advice is to use <code>#define<\/code> to create constants, which is common and expected. When you use <code>#define<\/code> to shorten tangled code, that might be another good use, but my advice is to pull back on creating new function-like macros and otherwise obfuscating the text. While such antics may impress hard core C programmers, the end result is difficult to document and debug.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Indeed, macros can get complicated when you stir variables into the mix. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1928\">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-1928","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\/1928","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=1928"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1928\/revisions"}],"predecessor-version":[{"id":1940,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1928\/revisions\/1940"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}