{"id":173,"date":"2013-07-20T00:01:52","date_gmt":"2013-07-20T08:01:52","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=173"},"modified":"2013-07-13T11:54:57","modified_gmt":"2013-07-13T19:54:57","slug":"what-is-true","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=173","title":{"rendered":"What is True?"},"content":{"rendered":"<p>True is a logical condition. It&#8217;s often written <code>TRUE<\/code> just to confuse it with &#8220;true&#8221; which is a word that has other implications. But for programming, <code>TRUE<\/code> generally means success. It&#8217;s the opposite of <code>FALSE<\/code>, which generally means failure.<br \/>\n<!--more--><br \/>\nTraditionally, <code>FALSE<\/code> is assigned the value zero. That&#8217;s not a hard fact, but it&#8217;s often the case.<\/p>\n<p>When a function fails, it often returns zero. Or, more commonly, loops wind down from some number to zero, meaning that the loop has done or, specifically, the condition upon which the loop repeats is now <code>FALSE<\/code>.<\/p>\n<p><code>TRUE<\/code>, on the other hand, is defined as non-zero. It could be the value 1, which it often is, but it could also be any other value that&#8217;s not zero. In fact, in many of my C programs, I&#8217;ll define <code>TRUE<\/code> and <code>FALSE<\/code> this way:<\/p>\n<p><code>#define FALSE 0<br \/>\n#define TRUE !FALSE<\/code><\/p>\n<p>So <code>FALSE<\/code> is zero and <code>TRUE<\/code> is &#8220;not <code>FALSE<\/code>.&#8221; That way the compiler can figure out what the value of TRUE is and my code still works. This method also frees you from having to bind the value TRUE to something specific.<\/p>\n<p>As an example, suppose one compiler uses the value 0x0001 for <code>TRUE<\/code> but another uses 0xFFFF for <code>TRUE<\/code>. It happens. Or, more commonly, some function interprets any non-zero value as <code>TRUE<\/code>. So 0x54 could be <code>TRUE<\/code>, as could 0x52, 0x55, and 0x45.<\/p>\n<p>Here&#8217;s a chunk of code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define FALSE 0\r\n#define TRUE !FALSE\r\n\r\nint main()\r\n{\r\n    printf(\"True is %d\\n\",TRUE);\r\n    printf(\"False is %d\\n\",FALSE);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>So how does the compiler calculate the value of <code>TRUE<\/code>? Here&#8217;s the output:<\/p>\n<p><code>True is 1<br \/>\nFalse is 0<\/code><\/p>\n<p>That makes sense from a logical point of view, but also because the <code>!<\/code> (bang or &#8220;not&#8221;) is a logical operator. The <em>not<\/em> value of zero is one.<\/p>\n<p>Other ways to compute TRUE and FALSE also prove out those values:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"True is %d\\n\",1==1);\r\n    printf(\"False is %d\\n\",1!=1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the above code, <code>TRUE<\/code> and <code>FALSE<\/code> aren&#8217;t declared as constants. They don&#8217;t have to be; the logical operations <code>1==1<\/code> and <code>1!=1<\/code> are used instead. The first operation is <code>TRUE<\/code> and the second is <code>FALSE<\/code>. The output is the same as for the first code:<\/p>\n<p><code>True is 1<br \/>\nFalse is 0<\/code><\/p>\n<p>The only exception I can find to the <code>TRUE<\/code> is 1 and <code>FALSE<\/code> is 0 paradigm is the return value passed to the operating system. In that case, zero has traditionally indicated a successful completion and any other value indicates some other condition.<\/p>\n<p>The return code zero as <code>TRUE<\/code> makes sense if you read the value returned as an &#8220;error level&#8221; instead of a success: Returning zero indicates a <code>FALSE<\/code>, or no error. That&#8217;s my explanation, although I&#8217;d be interested in the history behind the return code to discover the real reason.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basic concepts of <code>TRUE<\/code> and <code>FALSE<\/code> mean more than just 1 or 0 &mdash; if they even mean that at all! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=173\">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-173","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\/173","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=173"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions\/182"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}