Thread: Is the value of an unsigned int less than the value of a signed char?

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    1

    Question Is the value of an unsigned int less than the value of a signed char?

    The following code gives the output as "x<=y", though I expected it to be "x>y". Why is it so?

    Code:
        unsigned x=1;
        signed char y=-1;
        if (x>y)
            printf("x>y\n");
        else
            printf("x<=y\n");

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    automatic casting.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by stahta01 View Post
    automatic casting.
    Or rather, no automatic casting. The result is x>y if you put a cast in the if like this: if ((signed char)x > y)

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: The compiler cast the y to unsigned! That is the cause of the issue.

    I suggest reading up on the rules the compiler follows on size promotions. Note: The rules may be only suggestions.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    gcc gives the following warning:
    Code:
    badunsigned.c:7:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (x > y)
               ^
    The reason for the warning is the strange behaviour when comparing a negative signed value and an unsigned value. The negative value is converted to an unsigned value 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"

    One more than the maximum value of a (4-byte) unsigned is 4294967296, and adding it to -1 gives 4294967295. So the comparison becomes
    Code:
    if (1 > 4294967295)
    (In reality it's "as if" 4294967296 is added. Really the 2's complement bit pattern of the signed char value of -1 is sign-extended to 4-bytes and then that bit pattern (all 1's) is simply interpreted as an unsigned value.)

    So you should always fix that warning. In this case you could:
    Code:
    if ((int)x > y)
    But really, you need to be questioning why you would be comparing signed and unsigned values in the first place.

    From C11 draft:
    6.3.1.3 Signed and unsigned integers
    1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
    2 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. 60)
    3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
    Last edited by john.c; 04-01-2018 at 08:34 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. Signed/Unsigned char issues
    By NewbGuy in forum C Programming
    Replies: 14
    Last Post: 07-11-2009, 02:27 PM
  3. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  4. unsigned and signed char
    By P.Phant in forum C Programming
    Replies: 3
    Last Post: 07-12-2003, 05:00 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM

Tags for this Thread