{"id":150,"date":"2017-11-03T12:27:37","date_gmt":"2017-11-03T11:27:37","guid":{"rendered":"http:\/\/em-company.fr\/blog\/?p=150"},"modified":"2017-11-06T22:02:46","modified_gmt":"2017-11-06T21:02:46","slug":"nested-macros","status":"publish","type":"post","link":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/","title":{"rendered":"Nested C Preprocessor Macros"},"content":{"rendered":"<p>The C preprocessor and macros are one of the most useful elements for writing portable C\/C++ code . It even can\u00a0 be used to completely different purposes, like text processing.\u00a0 However, it suffers one major drawback: it does not support nested macros expansion (put differently, macros within macros).<\/p>\n<p><!--more--><\/p>\n<p>So for example, the following would not work as you expect:<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n\r\n#define OUTER_MACRO(a,b) (a ## b)\r\n#define INNER_MACRO(a) (#a)\r\n\r\nconst char *str1 = \"hello world\";\r\n\r\nvoid main (void)\r\n{\r\n  \/\/ Displays \"Hello world\"\r\n  printf (\"Output is: %s\\n\", OUTER_MACRO(st,r1)); \r\n\r\n  \/\/ Displays \"r1\"\r\n  printf (\"%s\", INNER_MACRO (r1)); \r\n \r\n  \/\/ .. but this one causes compiler error\r\n  printf (\"Output is: %s\", OUTER_MACRO (st, INNER_MACRO (r1))); \r\n}<\/pre>\n<p>Both OUTER_MACRO and INNER_MACRO work as expected, but not in composition. There are however situations where this is desirable.<\/p>\n<p>For example, in the following code:<\/p>\n<pre>*fp = fopen (ACCEL_DATA_FILE, \"r\");<\/pre>\n<p>ACCEL_DATA_FILE refers to, as the name suggest, some acceleration data gathered by an instrumentation. As we are interested that the file name for these &#8220;Acceleration data&#8221; reflect also some date\/time, we need to find a way to once open <em>accel_data_20171013.dat\u00a0<\/em>and later <em>accel_data_1027104.dat<\/em>. However, our client code should not change.This can be achieved with a smart macro toolbox, as follows.<\/p>\n<p>First, we define some utility macros (tools_macros.h)<\/p>\n<pre>#ifndef __BIZARRE_MACROS__\r\n#define __BIZARRE_MACROS__\r\n\r\n#define STRINGIFY(X) STRINGIFY2(X)\r\n#define STRINGIFY2(X) #X\r\n\r\n#define CAT(X,Y) CAT2(X,Y)\r\n#define CAT2(X,Y) X##Y\r\n\r\n#define INCLUDE_FILE(HEAD,TAIL) STRINGIFY(CAT(HEAD,TAIL) )\r\n\r\n#endif<\/pre>\n<p>The whole secret here is to do a &#8220;double expansion&#8221;, so that, contrary to the small test program above, each level of expansion produces something meaningful.<\/p>\n<p>Now we can write<\/p>\n<pre>#include \"tools_macros.h\"\r\n\r\n#define REL_PATH .\\\\data\r\n#define ACCEL_DATA_FILE STRINGIFY(CAT(REL_PATH,\\\\session_accel.c))\r\n\r\n<\/pre>\n<p>the outer macro would expand as<\/p>\n<p><em>&#8220;CAT(REL_PATH, \\\\session_accel.c)&#8221;<\/em><\/p>\n<p>so our line <em>fopen<\/em> would then include one single macro which expands correctly :). Following the same principle, we can also write:<\/p>\n<pre>const int16_t a_test_data[] = {\r\n\r\n#include INCLUDE_FILE(SESSION_PREFIX,_accel.c)\r\n\r\n};<\/pre>\n<p>where the inner generated file contains only data lines<\/p>\n<pre>-1689,1481,288,\r\n-1526,1605,168,\r\n-1564,1063,-417,\r\n-1677,621,-827,\r\n-1737,1157,-243,\r\n-1589,1194,-637,\r\n-1364,790,-584,\r\n-1420,924,-541,\r\n-1610,1386,-453,\r\n-1534,1361,-494,\r\n-1462,1249,-632,\r\n-1487,1354,-551,\r\n-1375,1429,-509,\r\n-1333,1413,-534,\r\n-1237,1359,-623,\r\n-1424,1350,-586,\r\n-1329,1548,-674,\r\n-1352,1472,-841,\r\n-1482,1601,-1215,\r\n-1405,2517,-804,\r\n-1339,3469,378,\r\n-1690,2384,-218,<\/pre>\n<p>Those lines are the typical output of some instrumentation (X,Y,Z accelerations). Using these tricky macros once again allowed us to reach the code \/ data isolation paradigm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C preprocessor and macros are one of the most useful elements for writing portable C\/C++ code . It even can\u00a0 be used to completely different purposes, like text processing.\u00a0 However, it suffers one major drawback: it does not support nested macros expansion (put differently, macros within macros).<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[24],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-programming","tag-macros"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nested C Preprocessor Macros - Embedded<\/title>\n<meta name=\"description\" content=\"Some advanced preprocessor technique to allow nested macros expansion\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nested C Preprocessor Macros - Embedded\" \/>\n<meta property=\"og:description\" content=\"Some advanced preprocessor technique to allow nested macros expansion\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/\" \/>\n<meta property=\"og:site_name\" content=\"Embedded\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-03T11:27:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-06T21:02:46+00:00\" \/>\n<meta name=\"author\" content=\"kriko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kriko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/\"},\"author\":{\"name\":\"kriko\",\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/#\\\/schema\\\/person\\\/fcb297f759038f0e8cb969c4c29742df\"},\"headline\":\"Nested C Preprocessor Macros\",\"datePublished\":\"2017-11-03T11:27:37+00:00\",\"dateModified\":\"2017-11-06T21:02:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/\"},\"wordCount\":267,\"commentCount\":0,\"keywords\":[\"macros\"],\"articleSection\":[\"programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/\",\"url\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/\",\"name\":\"Nested C Preprocessor Macros - Embedded\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/#website\"},\"datePublished\":\"2017-11-03T11:27:37+00:00\",\"dateModified\":\"2017-11-06T21:02:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/#\\\/schema\\\/person\\\/fcb297f759038f0e8cb969c4c29742df\"},\"description\":\"Some advanced preprocessor technique to allow nested macros expansion\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/index.php\\\/2017\\\/11\\\/03\\\/nested-macros\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nested C Preprocessor Macros\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/\",\"name\":\"Embedded\",\"description\":\"(...but not only)\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.em-company.fr\\\/blog\\\/#\\\/schema\\\/person\\\/fcb297f759038f0e8cb969c4c29742df\",\"name\":\"kriko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nested C Preprocessor Macros - Embedded","description":"Some advanced preprocessor technique to allow nested macros expansion","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/","og_locale":"en_US","og_type":"article","og_title":"Nested C Preprocessor Macros - Embedded","og_description":"Some advanced preprocessor technique to allow nested macros expansion","og_url":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/","og_site_name":"Embedded","article_published_time":"2017-11-03T11:27:37+00:00","article_modified_time":"2017-11-06T21:02:46+00:00","author":"kriko","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kriko","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/#article","isPartOf":{"@id":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/"},"author":{"name":"kriko","@id":"https:\/\/www.em-company.fr\/blog\/#\/schema\/person\/fcb297f759038f0e8cb969c4c29742df"},"headline":"Nested C Preprocessor Macros","datePublished":"2017-11-03T11:27:37+00:00","dateModified":"2017-11-06T21:02:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/"},"wordCount":267,"commentCount":0,"keywords":["macros"],"articleSection":["programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/","url":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/","name":"Nested C Preprocessor Macros - Embedded","isPartOf":{"@id":"https:\/\/www.em-company.fr\/blog\/#website"},"datePublished":"2017-11-03T11:27:37+00:00","dateModified":"2017-11-06T21:02:46+00:00","author":{"@id":"https:\/\/www.em-company.fr\/blog\/#\/schema\/person\/fcb297f759038f0e8cb969c4c29742df"},"description":"Some advanced preprocessor technique to allow nested macros expansion","breadcrumb":{"@id":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.em-company.fr\/blog\/index.php\/2017\/11\/03\/nested-macros\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.em-company.fr\/blog\/"},{"@type":"ListItem","position":2,"name":"Nested C Preprocessor Macros"}]},{"@type":"WebSite","@id":"https:\/\/www.em-company.fr\/blog\/#website","url":"https:\/\/www.em-company.fr\/blog\/","name":"Embedded","description":"(...but not only)","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.em-company.fr\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.em-company.fr\/blog\/#\/schema\/person\/fcb297f759038f0e8cb969c4c29742df","name":"kriko"}]}},"_links":{"self":[{"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":11,"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.em-company.fr\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}