Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Welcome to <a href="http://www.idryman.org/blog/2012/11/21/integer-promotion/" rel="noreferrer">integer promotion</a>. If I may quote from the website:</p> <blockquote> <p>If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.</p> </blockquote> <p>C can be really confusing when you do comparisons such as these, I recently puzzled some of my non-C programming friends with the following tease:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char* string = "One looooooooooong string"; printf("%d\n", strlen(string)); if (strlen(string) &lt; -1) printf("This cannot be happening :("); return 0; } </code></pre> <p>Which indeed does print <code>This cannot be happening :(</code> and seemingly demonstrates that 25 is smaller than -1! </p> <p>What happens underneath however is that -1 is represented as an unsigned integer which due to the underlying bits representation is equal to 4294967295 on a 32 bit system. And naturally 25 is smaller than 4294967295.</p> <p>If we however explicitly cast the <code>size_t</code> type returned by <code>strlen</code> as a signed integer:</p> <pre><code>if ((int)(strlen(string)) &lt; -1) </code></pre> <p>Then it will compare 25 against -1 and all will be well with the world.</p> <p>A good compiler should warn you about the comparison between an unsigned and signed integer and yet it is still so easy to miss (especially if you don't enable warnings).</p> <p>This is especially confusing for Java programmers as all primitive types there are signed. Here's what James Gosling (one of the creators of Java) <a href="http://www.gotw.ca/publications/c_family_interview.htm" rel="noreferrer">had to say on the subject</a>:</p> <blockquote> <p>Gosling: For me as a language designer, which I don't really count myself as these days, what "simple" really ended up meaning was could I expect J. Random Developer to hold the spec in his head. That definition says that, for instance, Java isn't -- and in fact a lot of these languages end up with a lot of corner cases, things that nobody really understands. Quiz any C developer about unsigned, and pretty soon you discover that almost no C developers actually understand what goes on with unsigned, what unsigned arithmetic is. Things like that made C complex. The language part of Java is, I think, pretty simple. The libraries you have to look up.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload