{"id":1188,"date":"2015-01-31T00:01:43","date_gmt":"2015-01-31T08:01:43","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1188"},"modified":"2015-01-23T09:51:08","modified_gmt":"2015-01-23T17:51:08","slug":"what-is-true-2","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1188","title":{"rendered":"What is True?"},"content":{"rendered":"<p>It happens so often, I&#8217;m curious as to why the C language standard I\/O header file doesn&#8217;t define TRUE and FALSE. Then again, what is TRUE and FALSE to a programming language &mdash; or to a computer? Why is this value true and that value false in the first place?<br \/>\n<!--more--><br \/>\nThe C language treats any non-zero value as a logical TRUE. The value zero is FALSE.<\/p>\n<p>Yes, even negative values are TRUE. This concept throws me. Once upon a time, I must have worked in a programming language where -1 was FALSE. That&#8217;s not the case in C, as the following code demonstrates.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int tf;\r\n\r\n    for(tf=-5;tf&lt;=5;tf++)\r\n    {\r\n        if(tf)\r\n            printf(\"%d is TRUE\\n\",tf);\r\n        else\r\n            printf(\"%d is FALSE\\n\",tf);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>-5 is TRUE\r\n-4 is TRUE\r\n-3 is TRUE\r\n-2 is TRUE\r\n-1 is TRUE\r\n0 is FALSE\r\n1 is TRUE\r\n2 is TRUE\r\n3 is TRUE\r\n4 is TRUE\r\n5 is TRUE<\/code><\/pre>\n<p>I could have extended the range for variable <code>tf<\/code> to the entire <em>int<\/em> spectrum. The output would be similar, with millions of TRUE values and only one FALSE value, zero.<\/p>\n<p>The advantage of this arrangement is that you can use a single value as a condition in an <em>if<\/em> statement or <em>while<\/em> loop, or anywhere a comparison is required. Non-zero values evaluate to TRUE.<\/p>\n<p>If you&#8217;re going to declare TRUE and FALSE constants in your code, do it this way:<\/p>\n<pre><code>#define  FALSE   0\r\n#define  TRUE    !FALSE<\/code><\/pre>\n<p><code>FALSE<\/code> is always zero. <code>TRUE<\/code> is any value that is &#8220;not&#8221; zero, or <code>!FALSE<\/code>. If you define TRUE as equal to 1, then you can mess up some conditions elsewhere in the code where a value other than 1 is legitimately considered to be TRUE.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most values are considered TRUE; only one value is FALSE. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1188\">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-1188","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\/1188","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=1188"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1201,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1188\/revisions\/1201"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}