{"id":542,"date":"2022-08-30T15:03:05","date_gmt":"2022-08-30T15:03:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/datatypes-and-datatype-modifiers-in-c-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:05","modified_gmt":"2022-08-30T15:03:05","slug":"datatypes-and-datatype-modifiers-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/datatypes-and-datatype-modifiers-in-c-collection-of-common-programming-errors\/","title":{"rendered":"Datatypes and datatype modifiers in C-Collection of common programming errors"},"content":{"rendered":"<p>I am pretty new to C. I recently came across this piece of code in C:<\/p>\n<pre><code>#include \n\nint main()\n{\n        unsigned Abc = 1;\n        signed Xyz = -1;\n\n        if(AbcXyz)\n                printf(\"Great\");\n        else\n        if(Abc==Xyz)\n        printf(\"Equal\");\n        return 0;\n}\n<\/code><\/pre>\n<p>I tried running it and it outputs &#8220;Less&#8221;. How does it work? What is the meaning of unsigned Abc? I could understand unsigned char Abc, but simply unsigned Abc? I am pretty sure Abc is no data type! How(and Why?) does this work?<\/p>\n<ol>\n<li>\n<p>The default type in C is <code>int<\/code>. Therefore <code>unsigned<\/code> is a synonym for <code>unsigned int<\/code>.<\/p>\n<p>Singed integers are usually handled using twos complement. This means that the actual value for 1 is 0x0001 and the actual value for -1 is 0xFFFF.<\/p>\n<\/li>\n<li>\n<p>Two things are happening.<\/p>\n<ol>\n<li>\n<p>The default data type in C in int. Thus you have variables of type signed int and unsigned int.<\/p>\n<\/li>\n<li>\n<p>When and unsigned int and a signed int are used in an expression the signed int is converted to unsigned before the expression is evaluated. This will cause signed(-1) to turn into a very large unsigned number (due to 2&#8217;s complement representation).<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>As far as I know, the signed value gets promoted to an unsigned value and so becomes very large.<\/p>\n<\/li>\n<li>\n<p><code>int<\/code> is the &#8220;default&#8221; type in C. <code>unsigned Abc<\/code> means <code>unsigned int Abc<\/code> just like <code>long L<\/code> means <code>long int L<\/code>.<\/p>\n<p>When you have an expression that mixes signed and unsigned ints, the signed ints get automatically converted to unsigned. Most systems use two&#8217;s complement to store integers, so <code>(unsigned int)(-1)<\/code> is equal to the largest possible <code>unsigned int<\/code>.<\/p>\n<\/li>\n<li>\n<p><strike>Comparing signed and unsigned types result in <em>undefined behavior<\/em>. Your program can and will print different results on different platforms.<\/strike><\/p>\n<p>Please see comments.<\/p>\n<\/li>\n<li>\n<p>unsigned\/signed is just short specification for unsigned int\/signed int (source), so no, you don&#8217;t have variable with &#8220;no data type&#8221;<\/p>\n<\/li>\n<li>\n<p>The signed value will get promoted to unsigned and therefore it will be bigger than 1.<\/p>\n<\/li>\n<li>\n<p>Add the following line after signed Xyz = -1;<\/p>\n<pre>\nprintf(\"is Abc =&gt; %x less than Xyz =&gt; %x\\n\",Abc,Xyz);\n<\/pre>\n<p>and see the result for yourself.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:46:00. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am pretty new to C. I recently came across this piece of code in C: #include int main() { unsigned Abc = 1; signed Xyz = -1; if(AbcXyz) printf(&#8220;Great&#8221;); else if(Abc==Xyz) printf(&#8220;Equal&#8221;); return 0; } I tried running it and it outputs &#8220;Less&#8221;. How does it work? What is the meaning of unsigned Abc? [&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-542","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/542","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=542"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}