{"id":56,"date":"2004-08-23T00:37:54","date_gmt":"2004-08-23T08:37:54","guid":{"rendered":"\/?p=56"},"modified":"2009-12-05T03:23:36","modified_gmt":"2009-12-05T11:23:36","slug":"void-casts-in-c-arent-totally-useless","status":"publish","type":"post","link":"https:\/\/www.slimjimmy.com\/weblog\/archives\/2004\/08\/23\/void-casts-in-c-arent-totally-useless\/","title":{"rendered":"<code>(void)<\/code> casts in C aren&#8217;t totally useless."},"content":{"rendered":"<p>Sometimes if I don&#8217;t care about a C function&#8217;s return value, I explicitly discard it with a <code>(void)<\/code> cast:<\/p>\n<pre class=\"example\">\r\n(void) foo();\r\n<\/pre>\n<p>Why bother?  After all, isn&#8217;t the return value implicitly discarded simply by not assigning it to anything?<\/p>\n<p><strong>A <code>(void)<\/code> cast is a comment to the reader<\/strong> stating, &#8220;Yes, the author is aware that this function returns something, has considered the consequences of ignoring possible error values, and just doesn&#8217;t care.&#8221;<\/p>\n<p>Without the cast, the code says, &#8220;Did the author intentionally ignore the return value of this function call?  Is there some failure case the author forgot to handle?  Can something go wrong here?&#8221;<\/p>\n<p>Furthermore, if something does go wrong, the <code>(void)<\/code> cast marks a possible failure point.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes if I don&#8217;t care about a C function&#8217;s return value, I explicitly discard it with a (void) cast: (void) foo(); Why bother? After all, isn&#8217;t the return value implicitly discarded simply by not assigning it to anything? A (void) cast is a comment to the reader stating, &#8220;Yes, the author is aware that this [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[25],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c"],"_links":{"self":[{"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.slimjimmy.com\/weblog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}