{"id":3301,"date":"2018-09-29T00:01:12","date_gmt":"2018-09-29T07:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3301"},"modified":"2018-09-22T11:25:52","modified_gmt":"2018-09-22T18:25:52","slug":"fun-with-preprocessor-names","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3301","title":{"rendered":"Fun with Preprocessor Names"},"content":{"rendered":"<p>Your probably familiar with the <code>#include<\/code> and <code>#define<\/code> preprocessor directives. You may know a few more, which come in handy for certain coding issues. I have <a href=\"https:\/\/c-for-dummies.com\/blog\/?page_id=2\">a page<\/a> on this blog, which goes over some of the common directives, but it doesn&#8217;t cover them all. Specifically, it doesn&#8217;t cover the preprocessor names.<br \/>\n<!--more--><br \/>\nPreprocessor names are wonderful assistants, especially when it comes to debugging your code. They give you the same power that smart compilers (such as <em>clang<\/em>) have when it comes to pinpointing issues within a source code file.<\/p>\n<p>For example, the <code>__LINE__<\/code> macro expands into the specific line number in a source code file where it appears:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"This is line %d\\n\",__LINE__);\r\n    return(0);\r\n}<\/pre>\n<p>The <code>__LINE__<\/code> macro is used in Line 5 in the source code file. At compile time, it&#8217;s translated into the constant for the current line number, which is reflected in the program&#8217;s output:<\/p>\n<p><code>This is line 5<\/code><\/p>\n<p>Unlike other preprocessor directives, preprocessor names aren&#8217;t prefixed with a hash character, though they are embraced by double underscores. They need not appear on a line by themselves. They act more like constant expressions, though the values they report are related to items in the source code file.<\/p>\n<p>In this code, I show all five of the predefined name macros in the ANSI C standard:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"This file is named %s\\n\",__FILE__);\r\n    printf(\"It was compiled on %s at %s\\n\",\r\n            __DATE__,\r\n            __TIME__\r\n          );\r\n    printf(\"Here is the STDC constant: %d\\n\",__STDC__);\r\n    printf(\"This is line %d\\n\",__LINE__);\r\n    return(0);\r\n}<\/pre>\n<p>Along with <code>__LINE__<\/code> to show the source code line, <code>__FILE__<\/code> gives the source code filename, and <code>__DATE__<\/code> and <code>__TIME__<\/code> give the timestamp when the code was compiled. The <code>__STDC__<\/code> value is 1 when the compiler supports ISO Standard C.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>This file is named 0929b.c<br \/>\nIt was compiled on Sep 15 2018 at 10:41:52<br \/>\nHere is the STDC constant: 1<br \/>\nThis is line 11<\/code><\/p>\n<p>By the way, you use the <code>#line<\/code> preprocessor directive to alter the value reported by the <code>__LINE__<\/code> macro. I&#8217;m not certain why you&#8217;d want to do such a thing, but the option is available:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n#line 86\r\n    printf(\"This is line %d\\n\",__LINE__);\r\n    return(0);\r\n}<\/pre>\n<p>And the output:<\/p>\n<p><code>This is line 86<\/code><\/p>\n<p>The <code>#line<\/code> directive renumbers all subsequent lines. This change affects both the <code>__LINE__<\/code> macro as well as any errors reported by the compiler. Of course, you can use <code>#line<\/code> again to reset the number, but then you&#8217;re just being crazy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your probably familiar with the #include and #define preprocessor directives. You may know a few more, which come in handy for certain coding issues. I have a page on this blog, which goes over some of the common directives, but &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3301\">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-3301","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\/3301","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=3301"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3301\/revisions"}],"predecessor-version":[{"id":3312,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3301\/revisions\/3312"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}