{"id":6693,"date":"2024-12-07T00:01:59","date_gmt":"2024-12-07T08:01:59","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6693"},"modified":"2024-11-30T10:42:48","modified_gmt":"2024-11-30T18:42:48","slug":"what-is-that-defined-constants-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6693","title":{"rendered":"What is That Defined Constant&#8217;s Value?"},"content":{"rendered":"<p>The C language uses defined constants to represent consistent values across platforms. For example, the <code>PATH_MAX<\/code> value might be 260 on one system and 4096 on another. It&#8217;s not important to know the specific value, just use the defined constant and your code builds and runs on various systems (hopefully).<br \/>\n<!--more--><br \/>\nIf you&#8217;re a nerd who enjoys code spelunking, you might want to know the specific value of a defined constant. I&#8217;ve done so many times. For example, in a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1216\">previous blog post<\/a> I wrote code to locate and output the <code>PATH_MAX<\/code> value. I&#8217;ve written specific programs that just output a library&#8217;s version number. Call me crazy.<\/p>\n<p>Well, I am crazy because a simple shortcut exists to output the value of any defined constant. All you need to know is which header file defines the constant and then use this format at the command prompt:<\/p>\n<p><code>cpp -include <em>header<\/em>.h -dM \/dev\/null | grep '#define <em>DEFCONST<\/em>'<\/code><\/p>\n<p>The <em>cpp<\/em> program is the C preprocessor. It&#8217;s called by the compiler to prepare the source code file. Using the above format, substitute <code><em>header<\/em>.h<\/code> with the proper header file and <code><em>DEFCONST<\/em><\/code> with the defined constant name to view specific <code>#define<\/code> directives in the header file.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<p><code>$ cpp -include limits.h -dM \/dev\/null | grep '#define PATH_MAX'<br \/>\n#define PATH_MAX 4096<\/code><\/p>\n<p>The <code>PATH_MAX<\/code> defined constant is set to the value 4096 on this system. (It&#8217;s the maximum string size for a pathname.)<\/p>\n<p>Here&#8217;s how the command line works:<\/p>\n<p><strong>cpp<\/strong> is the C preprocessor.<\/p>\n<p><strong>-include<\/strong> directs the compiler to include the named header file.<\/p>\n<p><strong>limits.h<\/strong> is the header filename. If you&#8217;re scanning a header file in a subdirectory, such as <code>sys\/net\/ethernet.h<\/code>, specify the pathname.<\/p>\n<p><strong>-dM<\/strong> directs the compiler to generate a list of <em>#define<\/em> directives in the code.<\/p>\n<p><strong>\/dev\/null<\/strong> is the empty file, the &#8220;road to nowhere.&#8221; The <em>cpp<\/em> program requires a filename argument, though it still examines the header file specified.<\/p>\n<p>The output from this command so far generates a list of all <em>#define<\/em> directives in the header file. To find a specific one, you pipe the output through the <em>grep<\/em> utility:<\/p>\n<p><strong>| grep &#8220;#define PATH_MAX&#8221;<\/strong><\/p>\n<p>The match string must include the <em>#define<\/em> directive. Otherwise, the output includes all references to the defined constant, which may be more than needed.<\/p>\n<p>While this trick is great, and I use it a lot, it doesn&#8217;t help in your code when the defined constant is unavailable. Refer to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6646\">this post<\/a> for information on how to use <em>#ifdef<\/em> to test for a definition. You can use the precompiler directives to generate a different program based on whether the constant is available or not.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a quick and easy way to look up the value of any defined constant. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6693\">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-6693","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\/6693","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=6693"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6693\/revisions"}],"predecessor-version":[{"id":6729,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6693\/revisions\/6729"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}