{"id":1216,"date":"2015-02-21T00:01:19","date_gmt":"2015-02-21T08:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1216"},"modified":"2015-02-14T10:35:29","modified_gmt":"2015-02-14T18:35:29","slug":"finding-the-path_max","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1216","title":{"rendered":"Finding the PATH_MAX"},"content":{"rendered":"<p>Your code may be compiled on a PC, Mac, Linux system, or even some microcontroller. In each case, it helps to know the environment before you code. You can guess, but it&#8217;s better to use the defined constants &mdash; if they&#8217;re available to you.<br \/>\n<!--more--><br \/>\nWhat do you do when a constant isn&#8217;t available? Actually, what&#8217;s more important is being able to check whether or not a constant exists. To manage that feat, you use the preprocessor directives: <code>#ifdef<\/code>, <code>#else<\/code>, and <code>#endif<\/code>.<\/p>\n<p>If you&#8217;re just starting out with C coding, you probably haven&#8217;t used preprocessor directives beyond <code>#include<\/code> and the occasional <code>#define<\/code>. The <em>if-else<\/em> series of directives are easy to understand, providing you have a general concept of the <em>if-else<\/em> programming thing.<\/p>\n<p>As a real-life example, consider the constant <code>PATH_MAX<\/code>. It&#8217;s determines how many characters are allowed in a full pathname. The value differs between the various operating systems, so it&#8217;s best to use <code>PATH_MAX<\/code> to set the buffer size for pathnames, as opposed to just guessing.<\/p>\n<p>To confirm that the <code>PATH_MAX<\/code> constant is defined, use the <code>#ifdef<\/code> directive:<\/p>\n<pre><code>#ifdef PATH_MAX<\/code><\/pre>\n<p>When <code>PATH_MAX<\/code> is defined, the statements following <code>#ifdef<\/code> are compiled.<\/p>\n<p>When <code>PATH_MAX<\/code> isn&#8217;t defined, the <code>#else<\/code> directive takes over. The statements following <code>#else<\/code> are compiled when <code>PATH_MAX<\/code> is undefined.<\/p>\n<p>The <code>#endif<\/code> directive closes the decisions structure. It&#8217;s traditionally followed by a comment identifying the constant.<\/p>\n<p>The following code represents a real-life example of testing for the <code>PATH_MAX<\/code> constant, which should be defined in the <code>limits.h<\/code> header file:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;limits.h&gt;\r\n\r\nint main()\r\n{\r\n\r\n#ifdef PATH_MAX\r\n    printf(\"PATH_MAX is defined as %d.\\n\",PATH_MAX);\r\n#else\r\n    printf(\"PATH_MAX isn't defined on this system.\\n\");\r\n#endif <span class=\"comments\">\/* PATH_MAX *\/<\/span>\r\n\r\n    return(0);\r\n}<\/pre>\n<p>When the <code>PATH_MAX<\/code> variable is defined, the first <em>printf()<\/em> statement is compiled, otherwise the second one is compiled. Remember, preprocessor directives don&#8217;t execute when the program runs; they simply tell the compiler which part of the code to include in the final program.<\/p>\n<p>Here&#8217;s the output for my Macintosh:<\/p>\n<pre><code>PATH_MAX is defined as 1024.<\/code><\/pre>\n<p>Here&#8217;s the output on my PC:<\/p>\n<pre><code>PATH_MAX is defined as 260.<\/code><\/pre>\n<p>The differing values justify why using a constant like <code>PATH_MAX<\/code> is important. The alternative would be to guess and specify a &#8220;safe&#8221; value for the constant. That&#8217;s a valid option only when you&#8217;ve tried testing for a constant as shown in this Lesson&#8217;s sample code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>And how to deal with the situation when a constant isn&#8217;t defined. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1216\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1216","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\/1216","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=1216"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":1224,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions\/1224"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}