{"id":6226,"date":"2024-02-03T00:01:49","date_gmt":"2024-02-03T08:01:49","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6226"},"modified":"2024-01-27T11:23:50","modified_gmt":"2024-01-27T19:23:50","slug":"a-nifty-little-test","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6226","title":{"rendered":"A Nifty Little Test"},"content":{"rendered":"<p>When I teach C programming, I&#8217;m careful to admonish beginners about the difference between the <code>=<\/code> (assignment) and <code>==<\/code> (is equal to) operators. Yet there are times when these two operators collaborate.<br \/>\n<!--more--><br \/>\nThe single equal sign is the assignment operator, used in expressions to set a value:<\/p>\n<p><code>a = 32;<\/code><\/p>\n<p>All too often, this operator is mistakenly used as a comparison:<\/p>\n<p><code>if(a=32)<\/code><\/p>\n<p>Most compilers flag this operation with a warning. Yep, you probably meant to write:<\/p>\n<p><code>if(a==32)<\/code><\/p>\n<p>Still, the improper format does result in a comparison. The expression <code>a=32<\/code> evaluates to 32, or whatever the assignment. In the world of C, 32 is a TRUE value, so the evaluation is TRUE.<\/p>\n<p>The expression <code>a==32<\/code> also results in a value, either 1 or 0 for TRUE and FALSE, respectively. This result is fully assignable, which you may find used in code.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_03-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_02_03-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b,c;\r\n\r\n    a = 7; b = 7;\r\n\r\n    c = a==b;\r\n    printf(\"%d==%d = %d\\n\",a,b,c);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In this code, the values of variables <code>a<\/code> and <code>b<\/code> are compared. The result of the comparison is assigned to variable <code>c<\/code>. The output shows which value is generated when two variables compare equally:<\/p>\n<p><code>7==7 = 1<\/code><\/p>\n<p>If you assign -7 to variable <code>b<\/code>, the output changes:<\/p>\n<p><code>7==-7 = 0<\/code><\/p>\n<p>This code proves that the result of a comparison operation is either one or zero, TRUE or FALSE. This assign is often used in code to test and store the result of a comparison. It allows you to use the result multiple times. Or you could use the value immediately. For example:<\/p>\n<p><code>return(a==b);<\/code><\/p>\n<p>The above statement returns either one or zero, depending on the values of variables <code>a<\/code> and <code>b<\/code>. It&#8217;s a clever approach to what would otherwise be written:<\/p>\n<p><code>if( a==b )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return(1);<br \/>\nelse<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return(0);<\/code><\/p>\n<p>Either approach works and generates the same result, but <code>return(a==b)<\/code> is clever and will impress others who read you code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Evaluating the result of a comparison. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6226\">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-6226","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\/6226","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=6226"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6226\/revisions"}],"predecessor-version":[{"id":6235,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6226\/revisions\/6235"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}