{"id":3553,"date":"2019-04-06T00:01:03","date_gmt":"2019-04-06T07:01:03","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3553"},"modified":"2019-04-13T10:58:52","modified_gmt":"2019-04-13T17:58:52","slug":"macros-or-functions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3553","title":{"rendered":"Macros or Functions"},"content":{"rendered":"<p>In C, a macro is a preprocessor directive that creates a shortcut, often a one-line expression to be inserted elsewhere in the code. Macros can make code more readable, but they can have a negative effect when implemented in a clumsy manner.<br \/>\n<!--more--><br \/>\nIn the following code, you see the age-old <em>max()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint max(x,y)\r\n{\r\n    if( x &gt; y )\r\n        return(x);\r\n    else\r\n        return(y);\r\n}\r\n\r\nint main()\r\n{\r\n    int a,b;\r\n\r\n    printf(\"Input two numbers separated by a space: \");\r\n    scanf(\"%d %d\",&amp;a,&amp;b);\r\n\r\n    printf(\"%d is greater\\n\",max(a,b));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Input two numbers separated by a space: 26 88<br \/>\n88 is greater<\/code><\/p>\n<p>The <em>max()<\/em> function can also be written:<\/p>\n<pre class=\"screen\">\r\nint max(x,y)\r\n{\r\n    return( x &gt; y ? x : y );\r\n}<\/pre>\n<p>The output is the same. Yet, something about a one-line function seems odd to me. In this Lesson&#8217;s example, the function isn&#8217;t required, but assume <em>max()<\/em> is part of a longer program where it&#8217;s called from multiple parts of the code. Even then, a one-line function seems rather silly.<\/p>\n<p>As an alternative, and with no particular advantage one way or another, you can declare <em>max()<\/em> as a macro. Use the <code>#define<\/code> preprocessor directive at the start of the source code file:<\/p>\n<p><code>#define max(x,y) x &gt; y ? x : y<\/code><\/p>\n<p>To create this macro, I scrunched up the <em>max()<\/em> function, removing the braces and semicolon at the end of the statement. What&#8217;s left is the macro <em>max()<\/em>, which takes two arguments, <code>x<\/code> and <code>y<\/code>. The arguments are expanded into the ternary operator are shown above: The macro itself is <em>max(x,y)<\/em> (no spaces) and everything on the rest of the line is the macro&#8217;s definition, what will be stuck into the code where the macro appears.<\/p>\n<p>Remember: Many of the &#8220;functions&#8221; you regularly use in C are defined as macros, such as <em>getchar()<\/em> and <em>putchar()<\/em>. Still, they look like and operate like functions within the code. Technically, they&#8217;re macros.<\/p>\n<p>In practice, I define my macros as ALL CAPS, just like defined constants. That way I know that the &#8220;function&#8221; is really a macro. Also, I feel it clues in anyone else reading my code that <em>MAX()<\/em> is a macro, and not a function they must hunt for elsewhere in the source code.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3563\">next week&#8217;s Lesson<\/a>, I use various macros to carry out mathematical operations at the binary level. It&#8217;s this kind of nerdy joy that makes me appreciate the C language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often it&#8217;s easy to express a single-line function as a macro. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3553\">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-3553","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\/3553","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=3553"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3553\/revisions"}],"predecessor-version":[{"id":3573,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3553\/revisions\/3573"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}