{"id":6324,"date":"2024-04-08T00:01:25","date_gmt":"2024-04-08T07:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6324"},"modified":"2024-04-06T11:03:04","modified_gmt":"2024-04-06T18:03:04","slug":"calculating-the-absolute-value-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6324","title":{"rendered":"Calculating the Absolute Value &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6313\">Exercise<\/a> may not have been challenging. Still, remember that blog is about learning the C language. Sometimes performing what could be a simple task may end up being complex than you anticipated.<br \/>\n<!--more--><br \/>\nSadly, this challenge probably wasn&#8217;t that complex. Still, it opens an insight into a programming quirk you may not have expected. Before revealing the quirk, here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_04-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_04-Exercise.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;\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%d\",&amp;a);\r\n\r\n    if( a&lt;0 )\r\n        a = -a;\r\n\r\n    printf(\"The absolute value is %d\\n\",a);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The code prompts for an integer value, <em>int<\/em> <code>a<\/code>. If the value is less than zero &mdash; meaning that it&#8217;s negative &mdash; the negation operator flips the value positive: <code>a = -a<\/code>. This expression isn&#8217;t the same as <code>a -= a<\/code>, which would always be zero. No, it negates the negative, turning it positive. The result is output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter an integer: -200<br \/>\nThe absolute value is 200<\/code><\/p>\n<p>And now for the quirk:<\/p>\n<p><code>Enter an integer: -2147483648<br \/>\nThe absolute value is -2147483648<\/code><\/p>\n<p>When a negative value is input at the bottom of the signed integer&#8217;s range, the result is the same negative value.<\/p>\n<p>The range for a 32-bit signed <em>int<\/em> data type in C is from -2,147,483,648 to 2,147,483,647. Due to the way negative values are expressed in binary, you cannot flip the lowest negative value to a positive as it would be out of range: The upper limit of an <em>int<\/em> data type is 2,147,483,647, not 2,147,483,648.<\/p>\n<p>This effect holds for the C library&#8217;s <em>abs()<\/em> function as well: If you recode the solution to use <em>abs()<\/em> (which requires including the <code>stdlib.h<\/code> header file), you get the same results.<\/p>\n<p>Here&#8217;s the nerdy part: When incremented, the 32-bit value 2,147,483,647 becomes -2,147,483,648. Therefore, the absolute value of -2,147,483,648 cannot be calculated as it&#8217;s beyond the range of a signed 32-bit integer.<\/p>\n<p>I hope you enjoyed the challenge and their weird aspect of finding the absolute value of an integer&#8217;s negative minimum.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise may not have been challenging. Still, remember that blog is about learning the C language. Sometimes performing what could be a simple task may end up being complex than you anticipated.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6324","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6324","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=6324"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6324\/revisions"}],"predecessor-version":[{"id":6356,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6324\/revisions\/6356"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}