Comparing C char and int

The code block below shows which fuzzy conversion occurs in the if statement for 7? I would get at least (0x98 <= 0x07), but this is not what happens when the condition evaluates to true and calls the DoMyStuff call.

char minstogo = 0x98; if(minstogo <= 7) { DoMyStuff(); } 
+4
source share
6 answers

Whenever you have a binary operator (one of + - * / % << >> & | ^ == != <= >= ) Between two integer operands of different types, two types are converted to a common type before execution operations. Rules for deciding on a converted type (from section 6.3.1.8 of the C99 standard):

If both operands are of the same type, then further conversions are not required.

Otherwise, if both operands are of integer types or both have unsigned integer types, the operand with the type of a lower integer conversion rank is converted to the operand type with a higher rank.

Otherwise, if the operand with an unsigned integer type has a rank greater than or equal to the ranks of the type of another operand, then the operand with an integer type with a sign is converted to the type of the unsigned operand of an integer type.

Otherwise, if the type of the operand with a signed integer type can represent all values โ€‹โ€‹of the type of the operand with an unsigned integer type, then the operand with an unsigned integer is converted to the operand type with a signed integer type.

Otherwise, both operands are converted to an unsigned integer type corresponding to the type of the operand with a signed integer type.

In this case, char can be either an integer type with a signature, or unsigned - its signature is determined by the implementation. Fortunately, int can represent all possible values โ€‹โ€‹of a char , regardless of whether char signed or not, if you are on a system where char is 8 bits and int is at least 16 bits.

If the char sign is signed, then the second paragraph above is applied, so both operands are converted to int (type with a higher rank, the rank is determined in a somewhat complicated way, but is essentially equivalent to the size of the type bit). Since 0x98, like a signed char , is negative, it converts to the integer -104, which is then less than 7.

If there is no character instead of char , then the fourth paragraph is used instead. An unsigned char will be converted to 152 as an int , which is greater than 7.

Never rely on char signed or unsigned. If you need 8-bit integers with a specific signature, I explicitly use signed char or unsigned char or use the C99 types int8_t and uint8_t defined by int <stdint.h> .

It is very easy to bite subtle mistakes caused by whole promotion rules. I highly recommend that you always compile with -Wall with gcc, which will warn you about comparing unsigned integers that are often the cause of errors.

+15
source

What can happen is that char is a signed value, and therefore 0x98 is registered as a negative number. Therefore, it is less than 7

Also in this scenario 7 will not be converted. Instead, char will be expanded to the same integral type as 7, and then a comparison will be made.

+6
source

With characters represented as an eight-bit byte, setting minstogo to 0x98 is a binary value of 10011000. The bit character set is a negative integer value. You probably need an unsigned char so the test can evaluate to false.

+1
source

It will evaluate the same as 0x98 <= 7 if the char platform type is not signed by default and CHAR_BIT is 8. In this case, minstogo will be negative and minstogo <= 7 will be true.

+1
source

Using your compiler with its current settings, char is a signed type: and since the high order bit (0x80) of its value is set, this value is negative. When minstogo expands, this negative sign is preserved (by expanding the sign), and therefore minstogo expands to a negative integer (e.g. 0xFF98), which is less than 7.

0
source

0x98 - 152.

Since you declared "char" and not "unsigned char", you are trying to assign 152 to a type that has a range of 128 - 127 .

This will overflow and give you a negative number that will be <7 (0x07).

0
source

Source: https://habr.com/ru/post/1286386/


All Articles