{"id":1921,"date":"2016-05-14T00:01:10","date_gmt":"2016-05-14T07:01:10","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1921"},"modified":"2016-05-21T08:41:36","modified_gmt":"2016-05-21T15:41:36","slug":"user-defined-functions-and-macros","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1921","title":{"rendered":"User-Defined Functions and Macros"},"content":{"rendered":"<p>When you desire to do something specific in your program, or have a chunk of code that&#8217;s duplicated elsewhere, you craft a function. That&#8217;s the traditional, problem-solving approach, and the C language is full of functions. Yet sometimes, programmers build macros instead of functions.<br \/>\n<!--more--><br \/>\nIn C programming, <em>macro<\/em> refers to a function created by using the <code>#define<\/code> <a href=\"http:\/\/c-for-dummies.com\/blog\/?page_id=2\">preprocessor directive<\/a>. In most of my sample code on this blog, <code>#define<\/code> creates a constant. The compiler expands the constant into whatever value is set, which is a handy way to control some limits within the code.<\/p>\n<p>The <code>#define<\/code> directive can also be used to replace symbols, which is bizarre but some programmers do it. And it&#8217;s used to build macros, which are like functions but typically shortcuts for more complex code elsewhere.<\/p>\n<p>For example, the old warhorse the <em>max()<\/em> function is presented in beginning programming lessons as a function that returns the larger of two values. This code can be written as a function, which might look like this:<\/p>\n<pre class=\"screen\">\r\nint max(int a, int b)\r\n{\r\n    if(a &gt; b)\r\n        return(a);\r\n    return(b);\r\n}<\/pre>\n<p>The sample above is just one way to solve the puzzle. You might also see a <code>max<\/code> macro, defined as follows:<\/p>\n<pre class=\"screen\">\r\n#define max( a, b) a &gt; b ? a : b<\/pre>\n<p>The macro defines <code>max<\/code> like a function, but it&#8217;s not a function. Instead, it&#8217;s a definition that the compiler populates throughout the code wherever the word <code>max<\/code> appears. Specifically, <code>max<\/code> would look like this:<\/p>\n<pre><code>max(100,2);<\/code><\/pre>\n<p>The compiler would substitute the above statement with the following:<\/p>\n<pre><code>100 &gt; 2 ? 100 : 2;<\/code><\/pre>\n<p>That&#8217;s ternary expression is evaluated as a value C language statement. The effect is the same as desired for a <em>max()<\/em> function, but the code is defined as a macro instead of written as a function.<\/p>\n<p>C programmers tend to adore the <code>#define<\/code> macros. In fact, a lot of the functions you use might be set as macros within the various header files. The man page hints at whether the &#8220;function&#8221; is actually a macro, though the only way to know for certain is to examine the header file. Even then not every compiler may use a macro; some may implement a true function instead.<\/p>\n<p>I tend to prefer functions over macros because macros are difficult to document and not as easy to read. Still, it&#8217;s kind of fun to craft a macro and use it in your code. As you can see from the samples above, it makes for a solution to interesting challenges. In the following code, the <em>charout()<\/em> function is actually a macro, defined at Line 3:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define charout( c ) fputc(c,stdout)\r\n\r\nint main()\r\n{\r\n    char text[] = \"Hello, there!\\n\";\r\n    int x = 0;\r\n\r\n    while(text[x])\r\n    {\r\n        charout(text[x]);\r\n        x++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>charout()<\/em> &#8220;function&#8221; at Line 12 is expanded by the compiler to equal:<\/p>\n<pre><code>fputc(text[x],stdout);<\/code><\/pre>\n<p>Of course, you could have written that line just as easily, but perhaps the programmer is more familiar with a <em>charout()<\/em> function from another language? Who knows! Still, you can use macros to simplify the way some code is presented. I&#8217;ll cover another example in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1928\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use the <code>#define<\/code> directive to build function macros. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1921\">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-1921","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\/1921","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=1921"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1921\/revisions"}],"predecessor-version":[{"id":1943,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1921\/revisions\/1943"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}