-3
#include<stdio.h>
void main()
{
    unsigned x = 1;
    signed char y = -1;
    if(x  > y)
        printf("x > y");
    else if(x == y)
        printf("x == y");
    else
        printf("x < y");
    printf("\n");
    printf("%d",(signed char)x);
    printf("\n");
    printf("%d",(unsigned int)y);
}

OUTPUT:
x < y
1
-1

I expected the output to be x == y as during comparison signed character is supposed to be converted to unsigned int? Please explain me how x < y...

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Please ___properly___ indent your code. Machine (Compiler) can read and compile anything, but for humans, it needs to make a little _sense_ while reading a block of text as _code_. When asking question, there was a big orange __How to Format__ box to the right of the text area. There was also an entire toolbar of formatting aids. And a __[?]__ button giving formatting help. And a preview area showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – Sourav Ghosh Nov 28 '17 at 10:00
  • 2
    Why would you expect x == y? Signed to unsigned doesn't mean "remove the sign" – Sami Kuhmonen Nov 28 '17 at 10:02

1 Answers1

3

I expected the output to be x == y as during comparison signed character is supposed to be converted to unsigned int?

Well, you're halfway there.

When a value of -1 is converted (promoted, actually) to unsigned int, the representation produces the biggest possible value representable by the type. Hence, the promoted value becomes greater than x which is 1.

Quoting C11, chapter §6.3.1.8, Usual arithmetic conversions

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

To clarify, the promotion does not mean, it removes the signedness. The operand (value), with the sign, is treated as the promoted type. The value is determined from the bit representation. The details: chapter §6.3.1.3,

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.


To add to above, the usage

printf("%d",(signed char)x);

and

printf("%d",(unsigned int)y);

are no good. %d expects a signed integer type (int) as argument.

  • If you want to print a signed char value, use %hhd
  • If you want to print an unsigned int, use %u
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Is there any way I could find that to what value is it converted? I tried type cast... – Insiyah_Hajoori Nov 28 '17 at 10:06
  • @Insiyah_Hajoori, well, you got to use the correct format specifier, but then, why do you want? – Sourav Ghosh Nov 28 '17 at 10:08
  • @Sourav Just add note about the misuse of `printf("%d",(unsigned int)y);`, and this is prefect answer.- – user694733 Nov 28 '17 at 10:13
  • Actually, you might want to replace *"The value is determined from the bit representation"* with excerpt from 6.3.1.3 *"Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type."* – user694733 Nov 28 '17 at 10:18