{"id":201,"date":"2014-11-02T18:20:22","date_gmt":"2014-11-02T18:20:22","guid":{"rendered":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/?p=201"},"modified":"2014-11-05T16:34:25","modified_gmt":"2014-11-05T16:34:25","slug":"strange-behaviors-with-characters","status":"publish","type":"post","link":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/?p=201","title":{"rendered":"Strange behaviors with characters"},"content":{"rendered":"<p>Some functions in the C code of Orchids use objects of type <code>char<\/code>, some others of type <code>unsigned char<\/code>. You will sometimes see strange sequences of casts in the code of Orchids, such as:<!--more--><\/p>\n<pre>{\r\n  unsigned char c;\r\n\r\n  \/* ... *\/\r\n  switch ((int)(unsigned int)c) {\r\n    \/* ... *\/\r\n  }\r\n}\r\n<\/pre>\n<p>To understand what is at stake, consider the following piece of code:<\/p>\n<pre>{\r\n  char c;\r\n\r\n  switch (c) {\r\n    \/* ... *\/\r\n    case '\u00e9': return 'e';\r\n    case '\u00e0': return 'a';\r\n    \/* ... *\/\r\n    default: return c;\r\n  }\r\n}\r\n<\/pre>\n<p>This appears to be some code whose purpose would be to remove accents from letters, but is completely buggy.<\/p>\n<p>One reason is that we accented characters such as <code>'\u00e9'<\/code> or <code>'\u00e0'<\/code> are system and compiler-dependent constants (more precisely, everything depends on the chosen <a title=\"character encoding\" href=\"https:\/\/en.wikipedia.org\/wiki\/Character_encoding\">character encoding<\/a>), and should not be used as such.<\/p>\n<p>However, the reason why I am giving that code as an example is to show an example of a very strange bug with characters in C.<\/p>\n<p>It is very likely that, if you start with code with <code>'\u00e9'<\/code> in <code>c<\/code>, it will <em>not<\/em> return <code>'e'<\/code>.<\/p>\n<p>Let me give the basic reasons for this strange behavior:<\/p>\n<ul>\n<li>The character constants such as <code>'\u00e9'<\/code> are of type <code>unsigned char0xe9<\/code> on my machine)<\/li>\n<li>The ANSI C stantard does not specify whether <code>char<\/code> should be <code>signed<\/code> or <code>unsigned<\/code>; let me assume that it is <code>signed<\/code>, as on many machines today<\/li>\n<li>The <code>switch<\/code> construct takes an object of type <code>int<\/code>, so implicit conversion from <code>char<\/code> to <code>int<\/code> took place in the code above.<\/li>\n<\/ul>\n<p>As a result, if you put <code>'\u00e9'<\/code> into <code>c<\/code>, <code>c<\/code> will contain the integer <code>0xe9<\/code>, namely <code>-0x17<\/code> as a <code>signed char; <code>switch<\/code> will convert it to the <code>int<\/code> value <code>-0x00000017 = 0xffffffe9<\/code>... and this is different from the value of the character <code>'\u00e9'<\/code> it compares it with.<\/code><\/p>\n<p>If <code>c<\/code> had been declared <code>unsigned char<\/code> instead, we might think the bug would have been averted. However, the path of conversions from <code>unsigned char<\/code> to (signed) <code>int<\/code> is not specified, as far as I know. If the C compiler first converts your <code>unsigned char<\/code> to <code>signed char<\/code> then to <code>int<\/code>, the same bug as above will occur.<\/p>\n<p>I prefer not to let the C compiler choose the sequence of conversions it should do, and I write them in full&#8230; as <code>(int)(unsigned int)c<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some functions in the C code of Orchids use objects of type char, some others of type unsigned char. You will sometimes see strange sequences of casts in the code of Orchids, such as:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-bugs-mean-bugs-and-c"],"_links":{"self":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201"}],"version-history":[{"count":2,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions\/210"}],"wp:attachment":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}