{"id":4027,"date":"2020-03-14T00:01:18","date_gmt":"2020-03-14T07:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4027"},"modified":"2020-03-07T09:15:38","modified_gmt":"2020-03-07T17:15:38","slug":"three-way-evaluations","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4027","title":{"rendered":"Three-Way Evaluations"},"content":{"rendered":"<p>Being traditional and, to be honest, ancient, the C language deals primarily with two-way evaluations: <code>a &gt; b<\/code>, <code>c != d<\/code>, <code>r &lt;= 0<\/code>, and so on. Complex comparisons build upon these atomic nuggets, but among the trendy languages a newer alternative exists: the three-way evaluation.<br \/>\n<!--more--><br \/>\nA two-way evaluation returns a Boolean, 1 or 0 or TRUE or FALSE. A three-way evaluation returns -1, 0, or 1 based on how items compare:<\/p>\n<p>-1 for less than<br \/>\n&nbsp;0 for equal<br \/>\n+1 for greater than<\/p>\n<p>The C language doesn&#8217;t have a three-way operator or other evaluation, but it does have the <em>strcmp()<\/em> function, upon which this three-way evaluation logic is based: The <em>strcmp()<\/em> function returns -1, 0, or 1 depending on how two strings compare. When all the characters match, zero is returned. Otherwise -1 or 1 are returned based on how the individual letters compare. A three-way evaluation follows the same logic.<\/p>\n<p>The following code uses the <em>tweval()<\/em> function to perform a three-way evaluation on two integers.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint tweval(int a, int b)\r\n{\r\n    if( a&lt;b )\r\n        return(-1);\r\n    if( a&gt;b )\r\n        return(1);\r\n    return(0);\r\n}\r\n\r\nint main()\r\n{\r\n    int x,y,r;\r\n\r\n    <span class=\"comments\">\/* get input *\/<\/span>\r\n    printf(\"Enter value 1: \");\r\n    scanf(\"%d\",&amp;x);\r\n    printf(\"Enter value 2: \");\r\n    scanf(\"%d\",&amp;y);\r\n\r\n    <span class=\"comments\">\/* three-way evaluation *\/<\/span>\r\n    r = tweval(x,y);\r\n    if( r&lt;0 )\r\n        printf(\"%d is less than %d\\n\",x,y);\r\n    else if( r&gt;0 )\r\n        printf(\"%d is greater than %d\\n\",x,y);\r\n    else\r\n        printf(\"%d and %d are equal\\n\",x,y);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>tweval()<\/em> function compares <code>a&lt;b<\/code> and returns -1 if true. It then compares <code>a&gt;b<\/code> and returns 1 if true. Otherwise, the function returns zero as the two integers must be equal.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter value 1: 5<br \/>\nEnter value 2: 100<br \/>\n5 is less than 100<\/code><\/p>\n<p>As is often the case, the C language is capable of emulating a feature offered in a language the cool kids favor, but what it doesn&#8217;t have is the common three-way operator, <code>&lt;=&gt;<\/code>, often called the &#8220;spaceship&#8221; operator. (The name comes from a text-mode <em>Star Trek<\/em> game way back when, where the <code>&lt;=&gt;<\/code> characters represented a spaceship.)<\/p>\n<p>So while you can&#8217;t use the <code>a &lt;=&gt; b<\/code> operator in C, you can concoct your own <em>tweval()<\/em> or similar function, copying from the <em>strcmp()<\/em> function &mdash; which has been in the C library for decades. And don&#8217;t forget to code three-way evaluation functions for other data types: <em>float<\/em>, <em>double<\/em>, and <em>char<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C language can lag behind other languages, but it can also provide inspiration. In the case of the three-way evaluation, it does both. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4027\">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-4027","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\/4027","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=4027"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4027\/revisions"}],"predecessor-version":[{"id":4035,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4027\/revisions\/4035"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}