{"id":940,"date":"2014-09-06T00:01:05","date_gmt":"2014-09-06T07:01:05","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=940"},"modified":"2014-08-30T08:07:55","modified_gmt":"2014-08-30T15:07:55","slug":"lovely-weird-assignment-operators","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=940","title":{"rendered":"Lovely, Weird Assignment Operators"},"content":{"rendered":"<p>Some programmers find it charming that C can be so frustratingly obscure. In fact, a lot of the language&#8217;s perplexing nature is simply a reflection of its efficiency.<br \/>\n<!--more--><br \/>\nAs a case in point, occasionally you need to modify the value in a variable. For example, suppose you need to add 4 to the value of variable <code>x<\/code>.<\/p>\n<p>One way to do that would be to create a helper variable <code>a<\/code>:<\/p>\n<pre><code>a = x + 4;\r\nx = a;<\/code><\/pre>\n<p>I&#8217;ll confess that I&#8217;ve used code like that. For some reason, my brain finds those two statements more acceptable than the straightforward solution, which is:<\/p>\n<pre><code>x = x + 4;<\/code><\/pre>\n<p>Because C evaluates the right side of the assignment first, the value of variable <code>x<\/code> is increased by 4, then the result is assigned back into variable <code>x<\/code>. It works, but it&#8217;s still not as efficient &#8212; or as cryptic &#8212; as it could be.<\/p>\n<p>The C language features shortcuts that let you modify a variable&#8217;s value. These are the <em>assignment operators<\/em>. You have five of them to choose from:<\/p>\n<p>Addition: <code>+=<\/code><br \/>\nSubtraction: <code>-=<\/code><br \/>\nMultiplication: <code>*=<\/code><br \/>\nDivision: <code>\/=<\/code><br \/>\nModulus: <code>%=<\/code><\/p>\n<p>They&#8217;re all charmingly obscure, but they&#8217;re also popular; you&#8217;ll find the same operators in just about every trendy programming language.<\/p>\n<p>These assignment operators exist to modify single variables, adding, subtracting, multiplying, dividing, or modulussing the variable&#8217;s existing value.<\/p>\n<p>Yes, <em>modulussing<\/em> is a word.<\/p>\n<p>The following code demonstrates how you could use the addition assignment operator to display values from 0 through 100 by increments of 4.<\/p>\n<pre class=\"screen\">#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x = 0;\r\n\r\n    while(x &lt; 100)\r\n    {\r\n        x += 4;\r\n        printf(\"%2d \",x);\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code could be made tighter by combining the equation at Line 9 with the <em>printf()<\/em> statement, which would also eliminate the need for curly brackets in the <em>while<\/em> loop. That would make it more mysterious, but less readable.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code> 4  8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100<\/code><\/pre>\n<p>By the way, the trick to remember the operators&#8217; order is simply to try them both ways:<\/p>\n<p><code>x += 4<\/code> makes sense for increasing variable <code>x<\/code> by 4. But:<\/p>\n<p><code>x =+ 4<\/code> assigns positive 4 the variable <code>x<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>That C operator may be cryptic, but it&#8217;s also immensely useful. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=940\">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-940","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\/940","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=940"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/940\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/940\/revisions\/954"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}