{"id":3986,"date":"2020-02-15T00:01:12","date_gmt":"2020-02-15T08:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3986"},"modified":"2020-02-08T08:36:33","modified_gmt":"2020-02-08T16:36:33","slug":"yet-another-oddball-token-paste","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3986","title":{"rendered":"Yet Another Oddball: Token Paste"},"content":{"rendered":"<p>I&#8217;m constantly on the prowl for obscure and seldom-used C language functions and techniques. One I just discovered &mdash; despite coding in C for decades &mdash; is the token paste operator. It&#8217;s a weirdo.<br \/>\n<!--more--><br \/>\nGoing back to the original K&#038;R (&#8220;the manual&#8221;), the token paste operator isn&#8217;t mentioned. No, you must go to the second edition of K&#038;R, published 10 years later in 1988, to discover the <code>##<\/code> preprocessor operator. Even then, the book is vague on specifics or explaining situations where the operator is handy or even necessary.<\/p>\n<p>The token paste operator glues two preprocessor varibles together. Also called the <em>pasting operator<\/em>, it&#8217;s used in a <code>#define<\/code> directive.<\/p>\n<p>As a review, <code>#define<\/code> creates a defined constant, a token that can be used throughout the source code file:<\/p>\n<p><code>#define SIZE 15<\/code><\/p>\n<p>The defined constant <code>SIZE<\/code> is changed to 15 before the code compiles. This constant is available to all functions in the file, whereas a constant integer <code>const int size=15;<\/code> would be available only within the function where it&#8217;s declared. (Also, <em>const int size<\/em> is a variable and <code>SIZE<\/code> is replaced before compiling.)<\/p>\n<p>The token paste operator finds its place in those macro constructions that emulate a function. A common example is <em>putchar()<\/em>, though this macro doesn&#8217;t use the token paste operator:<\/p>\n<p><code>#define putchar(a) putc(a,stdout)<\/code><\/p>\n<p>The <em>putchar()<\/em> &#8220;function&#8221; is really a macro, defined in the <code>stdio.h<\/code> header file similar to above, where the <em>putc()<\/em> function does the actual work. Variable <code>a<\/code> is lifted from <code>putchar(a)<\/code> and set into the <em>putc()<\/em> function as its first argument. This is one way <code>#define<\/code> directives build macros.<\/p>\n<p>The token paste operator works on the macro&#8217;s argument(s). It pastes or glues one argument to another or to a constant value. Here&#8217;s an example:<\/p>\n<p><code>#define paste(a,b) a##b<\/code><\/p>\n<p>Given arguments <code>a<\/code> and <code>b<\/code>, the <em>paste()<\/em> macro sticks them together as <code>ab<\/code> in your code. So <code>paste(p,i)<\/code> results in <code>pi<\/code>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define paste( a, b ) a##b\r\n\r\nint main()\r\n{\r\n    float pi = 3.141;\r\n\r\n    printf(\"%f\\n\",paste(p,i));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the <em>printf()<\/em> statement, <code>paste(p,i)<\/code> sticks the letters <code>p<\/code> and <code>i<\/code> together to form <code>pi<\/code>. This evaluation becomes <em>float<\/em> variable <code>pi<\/code>, which is output when the program runs.<\/p>\n<p>Here&#8217;s another, more interesting but still questionably useful example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define s1 \"Goodbye, \"\r\n#define s2 \"cruel world!\"\r\n\r\n#define output(x) s##x\r\n\r\nint main()\r\n{\r\n    printf(\"%s%s\\n\",\r\n            output(1),\r\n            output(2)\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Two defined constants, <code>s1<\/code> and <code>s1<\/code>, are created. The <em>output()<\/em> macro prefixes letter <code>s<\/code> to its argument <code>x<\/code>. So <code>output(1)<\/code> expands to <code>s1<\/code> and <code>output(2)<\/code> becomes <code>s2<\/code>. The <em>printf()<\/em> statement outputs the resulting strings:<\/p>\n<p><code>Goodbye, cruel world!<\/code><\/p>\n<p>I confess that these two examples are lifted from other websites where an attempt was made to describe how the token paste operator could perhaps be useful. Trying to find my own, clever demonstration code proved futile. Then again, the <code>##<\/code> operator is obscure for a reason.<\/p>\n<p>What would be interesting to me is to unravel how this operator came to be. It wasn&#8217;t documented in 1978 when the first K&#038;R book appeared. So what happened between then and 1988 when it appeared in the second edition? Was it borrowed from another programming language? Did something occur in computer history that necessitated the operator? Perhaps we&#8217;ll never know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The preprocessor ## operator is one of the most peculiar ones I&#8217;ve encountered. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3986\">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-3986","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\/3986","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=3986"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3986\/revisions"}],"predecessor-version":[{"id":3993,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3986\/revisions\/3993"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}