{"id":2542,"date":"2017-06-10T00:01:48","date_gmt":"2017-06-10T07:01:48","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2542"},"modified":"2017-06-03T11:55:58","modified_gmt":"2017-06-03T18:55:58","slug":"the-elvis-operator","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2542","title":{"rendered":"The Elvis Operator"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/06\/elvis-operator.png\" alt=\"\" width=\"213\" height=\"235\" class=\"alignnone size-full wp-image-2548\" \/><\/p>\n<p>A new operator was added to the C language in the last revision, one that I don&#8217;t cover in my books. (I&#8217;m not sure how that happened.) Anyway, it&#8217;s the Elvis operator. Unless you&#8217;re a fan of the ternary operator, you&#8217;ll probably never use it.<br \/>\n<!--more--><br \/>\nAs a review, the ternary operator is a delightfully cryptic way to represent a single <em>if-else<\/em> choice. You can take code such as this:<\/p>\n<pre class=\"screen\">\r\n   if(a &gt; b)\r\n       c = a;\r\n   else\r\n       c = b;<\/pre>\n<p>And transform it into this goober:<\/p>\n<pre class=\"screen\">\r\n    c = a &gt; b ? a : b;<\/pre>\n<p>In both code snippets, the greater of variables <code>a<\/code> or <code>b<\/code> is assigned to variable <code>c<\/code>.<\/p>\n<p>The ternary operator can&#8217;t replace all <em>if-else<\/em> statements, but it works well in certain situations. In my code, I generally do the <em>if-else<\/em> structure first and only later reduce it to the ternary operator &mdash; but only when such a simplifies the code. It&#8217;s rare that I first strive to use the ternary operator.<\/p>\n<p>The ternary operator has its own reduction, known as the Elvis operator: <code>?:<\/code> Apparently, enough people think of the <code>?:<\/code> characters as an Elvis Presley emoticon, so the operator is given his nickname.<\/p>\n<p>Whereas the ternary operator represents this statement:<\/p>\n<p><code><em>result<\/em> = <em>comparison\/text<\/em> ? <em>if_true<\/em> : <em>if_false<\/em>;<\/code><\/p>\n<p>The Elvis operator works like this:<\/p>\n<p><code><em>result<\/em> = <em>if_true<\/em> ?: <em>otherwise<\/em>;<\/code><\/p>\n<p>So the Elvis operator replaces the ternary operator only for specific conditions: When the first statement is true, that value is assigned. Otherwise the second value (or result) is assigned, whether that value is true or not.<\/p>\n<p>Here&#8217;s a typical Elvis operator statement:<\/p>\n<p><code>r = voltage() ?: 120;<\/code><\/p>\n<p>The value of variable <code>r<\/code> is set to the value returned by function <em>voltage()<\/em>. If the value returned is zero (<code>FALSE<\/code>), the value 120 is assigned instead. This statement is the equivalent of the following:<\/p>\n<pre class=\"screen\">\r\n    r = voltage();\r\n    if( r == 0)\r\n        r = 120;<\/pre>\n<p>In my code, I prefer to use the second set of statements because it&#8217;s readable. Still, the Elvis operator is available for those times you feel it&#8217;s necessary for obfuscation or it&#8217;s cool to use an operator named after a rock-and-roll legend.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint voltage(void)\r\n{\r\n    return(60);\r\n}\r\n\r\nint main()\r\n{\r\n    int r;\r\n\r\n    r = voltage() ?: 120;\r\n    printf(\"Voltage is %d\\n\",r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample run:<\/p>\n<pre><code>Voltage is 60<\/code><\/pre>\n<p>If you modify the <em>voltage()<\/em> function to return the value zero, then the output becomes:<\/p>\n<pre><code>Voltage is 120<\/code><\/pre>\n<p>I&#8217;m unsure of the Elvis operator&#8217;s origins. In C, it&#8217;s implemented as the ternary operator, where the second part is missing. So, effectively:<\/p>\n<p><code>r = voltage() ?: 120;<\/code><\/p>\n<p>is the same as<\/p>\n<p><code>r = voltage() ? voltage() : 120;<\/code><\/p>\n<p>Some programming languages have strict syntax for the Elvis operator, with others using a different set of symbols or logical expressions instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>?: Thank you. Thank you very much. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2542\">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-2542","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\/2542","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=2542"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2542\/revisions"}],"predecessor-version":[{"id":2553,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2542\/revisions\/2553"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}