<p>If I have: </p>

<pre><code>signed char * p;
</code></pre>

<p>and I do a comparison:</p>

<pre><code>if ( *p == 0xFF )
   break;
</code></pre>

<p>it will never catch 0XFF, but if I replace it with -1 it will:</p>

<pre><code>if ( *p == (signed char)0xFF )
   break;
</code></pre>

<p>How can this happen? Is it something with the sign flag? I though that <code>0xFF == -1 == 255</code>.</p>

[{'answer_id': 61229, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 6, 'selected': True, 'text': '<p>The value <code>0xFF</code> is a signed int value. C will promote the <code>*p</code> to an <code>int</code> when doing the comparison, so the first if statement is equivalent to:</p>\n\n<pre><code>if( -1 == 255 ) break;\n</code></pre>\n\n<p>which is of course false. By using <code>(signed char)0xFF</code> the statement is equivalent to:</p>\n\n<pre><code>if( -1 == -1 ) break;\n</code></pre>\n\n<p>which works as you expect. The key point here is that the comparison is done with <code>int</code> types instead of <code>signed char</code> types.</p>\n'}, {'answer_id': 61230, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 2, 'selected': False, 'text': '<p>It casts to an int for the first comparison since 0xFF is still considered an int, meaning your char is -128 to 127, but the 0xFF is still 255.</p>\n\n<p>In the second case your telling it that 0xFF is really an signed char, not an int</p>\n'}, {'answer_id': 61232, 'author': 'Niall', 'author_id': 6049, 'author_profile': 'https://Stackoverflow.com/users/6049', 'pm_score': 2, 'selected': False, 'text': '<p>Integer literals have signed int type. Since 0xFF is a signed int, the compiler converts *p to a signed int and then does the comparison.</p>\n\n<p>When *p is -1, which is then converted from a signed char to a signed int, it is still -1 which has a representation of 0xFFFFFFFF, which is not equal to 0xFF.</p>\n'}, {'answer_id': 61234, 'author': 'botismarius', 'author_id': 4528, 'author_profile': 'https://Stackoverflow.com/users/4528', 'pm_score': 1, 'selected': False, 'text': '<p>0xff will be seen as an integer constant, with the value of 255. You should always pay attention to these kind of comparison between different types. If you want to be sure that the compiler will generate the right code, you should use the typecast:</p>\n\n<pre>\nif( *p == (signed char)0xFF ) break;\n</pre>\n\n<p>Anyway, <b>beware</b> that the next statement will <b>not</b> work the same way:</p>\n\n<pre>\nif( (int)*p == 0xFF ) break;\n</pre>\n\n<p>Also, maybe it would be a better idea to avoid signed chars, or, it you must use signed chars, to compare them with signed values such as -1 in this case:</p>\n\n<pre>\nif( *p == -1 ) break;\n</pre>\n\n<p>0xff==-1 only if those values would be assigned to some char (or unsigned char) variables:</p>\n\n<pre>\nchar a=0xff;\nchar b=-1;\nif(a==b) break;\n</pre>\n'}]
['https://Stackoverflow.com/questions/61227', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2566/']