{"id":2150,"date":"2016-10-08T00:01:44","date_gmt":"2016-10-08T07:01:44","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2150"},"modified":"2016-10-01T13:36:50","modified_gmt":"2016-10-01T20:36:50","slug":"the-curse-of-typecasting-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2150","title":{"rendered":"The Curse of Typecasting &#8211; Solution"},"content":{"rendered":"<p>I hope you didn&#8217;t find this month&#8217;s Exercise too daunting. In fact, it&#8217;s pretty basic stuff, but often something you might forget. In fact, the compiler may remind you when you attempt to pass an argument to a function and it&#8217;s of the wrong type.<br \/>\n<!--more--><br \/>\nHere&#8217;s my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int apple = 65;\r\n\r\n    printf(\"As integer: %d\\n\",apple);\r\n    printf(\"As character: %c\\n\",(char)apple);\r\n    printf(\"As float: %f\\n\",(float)apple);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>apple<\/code> is assigned value 65 in Line 5. It&#8217;s an <em>int<\/em> variable and 65 is an integer value.<\/p>\n<p>Line 7 prints variable <code>apple<\/code>&#8216;s value as an integer; the <em>printf()<\/em> placeholder <code>%d<\/code> is used and the variable doesn&#8217;t need to be typecast.<\/p>\n<p>Line 8 typecasts <code>apple<\/code> to a <em>char<\/em> variable. The <code>%c<\/code> placeholder is used, therefore the value is interpreted as a character and displayed as such.<\/p>\n<p>Finally, Line 9 typecasts <code>apple<\/code> to a <em>float<\/em> value, with <code>%f<\/code> doing the heavy lifting to show the value as a real number.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>As integer: 65\r\nAs character: A\r\nAs float: 65.000000<\/code><\/pre>\n<p>Because a <em>char<\/em> variable is a type of integer, you don&#8217;t have to typecast in Line 8; you still get the same results. You must, however, typecast in Line 9. It&#8217;s the fact that you can treat a <em>char<\/em> as a character or integer that makes C a weak-type language. Other programming languages use stronger variable types, where you can&#8217;t treat a value of one type as another.<\/p>\n<p>Most frequently, you use typecasting when a specific variable type is required as a function argument. For example, the function needs an <em>int<\/em> but you&#8217;re using a <em>float<\/em>. In that instance, you must typecast the variable to match the requirements of the function.<\/p>\n<p>Typecasting becomes a big issue with pointer arguments as the function most definitely must know what type of storage lurks at the variable&#8217;s location in memory. Frequently, you&#8217;ll use a typecast with the <code>NULL<\/code> constant &mdash; which isn&#8217;t zero, but rather a pointer. You must typecast the <code>NULL<\/code> so that it matches the required variable type, such as <code>(char *)NULL<\/code>. Be on the lookout for those instances when typecasting is a must.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope you didn&#8217;t find this month&#8217;s Exercise too daunting. In fact, it&#8217;s pretty basic stuff, but often something you might forget. In fact, the compiler may remind you when you attempt to pass an argument to a function and &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2150\">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":[5],"tags":[],"class_list":["post-2150","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\/2150","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=2150"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2150\/revisions"}],"predecessor-version":[{"id":2168,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2150\/revisions\/2168"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}