{"id":673,"date":"2022-08-30T15:05:16","date_gmt":"2022-08-30T15:05:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/evaluation-on-the-right-side-of-an-assignment-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:16","modified_gmt":"2022-08-30T15:05:16","slug":"evaluation-on-the-right-side-of-an-assignment-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/evaluation-on-the-right-side-of-an-assignment-collection-of-common-programming-errors\/","title":{"rendered":"Evaluation on the right side of an assignment-Collection of common programming errors"},"content":{"rendered":"<p>This <code>int c = (a==b)<\/code> is exactly what I&#8217;d like to say in my C program, compiling with GCC. I can do it, obviously (it works just fine), but I don&#8217;t know whether it may cause undefined behavior. My program will not be compiled with some other compiler or in other architectures. Is this legal ANSI C? Thanks.<\/p>\n<ol>\n<li>\n<p>It&#8217;s completely legal. if <code>a<\/code> is equal to <code>b<\/code>, then <code>c<\/code> will be 1. else, it will be 0.<\/p>\n<\/li>\n<li>\n<pre><code>int c = (a == b);\n<\/code><\/pre>\n<p>this is perfectly legal. Initialization is part of the C standard (C99 \u00a76.7.8), the right hand side can just be any <em>assignment-expression<\/em>, including <code>a == b<\/code> (of course, assuming <code>a<\/code> and <code>b<\/code> are defined and have comparable type).<\/p>\n<\/li>\n<li>\n<p>It is perfectly valid if <code>c<\/code> is declared at block scope.<\/p>\n<p>When declared at file scope it is not valid because the initializer has to be a constant expression.<\/p>\n<p><code>a == b<\/code> is an expression and in that sense is not different that another expression like <code>a + b<\/code> or <code>a &amp; b<\/code>.<\/p>\n<\/li>\n<li>\n<p>Well, it depends on what the types of <code>a<\/code> and <code>b<\/code> are. If they are types that support equality check, then yes, it&#8217;s perfectly legal.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:23:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This int c = (a==b) is exactly what I&#8217;d like to say in my C program, compiling with GCC. I can do it, obviously (it works just fine), but I don&#8217;t know whether it may cause undefined behavior. My program will not be compiled with some other compiler or in other architectures. Is this legal [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-673","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=673"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/673\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}