Unsigned int and signed char comparison

I am trying to compare an unsigned int with a signed char as follows:

int main(){ unsigned int x = 9; signed char y = -1; x < y ? printf("s") : printf("g"); return 0; } 

I expected o / p to be "g". Instead, its "s". What is conversion here?

+4
source share
5 answers

Section 6.3.1.8 , Regular Arithmetic Conversions, C99 details implicit integer conversions.

If both operands are of the same type, then no further conversion is required.

This does not mean that these are different types.

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

This does not count as one of them is signed, the other is unsigned.

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

Bingo. x has a higher rank than y , so y is promoted to unsigned int . This means that it changes from -1 to UINT_MAX , significantly greater than 9.

The rest of the rules do not apply, since we found a match, but I will include them for completeness:

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, then the operand with an unsigned integer type will be converted to the type of the operand 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.


The ranks related to this issue are shown below. All ranks are described in detail in C99, section 6.3.1.1 , Boolean, character, and integers, so you can refer to this for more details.

The rank of long long int must be greater than the rank of long int , which must be greater than the rank of int , which must be greater than the rank of short int , which must be greater than the rank of signed char .

The rank of char must be equal to the ranks of signed char and unsigned char .

+23
source

My hunch y rises to unsigned int , which becomes a big value (due to wrapping). Therefore, the condition is satisfied.

+2
source

Enter the following code:

 int main(){ unsigned int x = 9; signed char y = -1; printf("%u\n", (unsigned int)y); x < (unsigned int)y ? printf("s") : printf("g"); return 0; } 

Output:

  4294967295
 s

After casting, y takes on a lot of importance. Therefore, the conclusion is s.

+2
source

char is assigned an unsigned int value with a MAX_UINT value that is greater than 9.

+1
source

Pay attention to the comment by caf at the link below: When you convert an out-of-range number to an unsigned type, it is entered into the range by repeatedly adding or subtracting more than the maximum value of the type -

C: convert signed unsigned

In this case, UINT_MAX + 1, which is 4294967296 on your platform, is added to -1 to give 4294967295.

+1
source

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


All Articles