2 unsigned shorts make a signed int ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode2
    New Member
    • Apr 2009
    • 32

    2 unsigned shorts make a signed int ???

    I have something funny happening when I compile (gcc)

    I have two variables:

    Code:
    unsigned short x;
    unsigned short y;

    and when I used them in a for loop condition:

    Code:
    unsigned int i;
    for( i = 0 ; i < x * y ; i++ )
    { ; }

    I get a compiler warning:
    " warning: comparison between signed and unsigned "

    so since my "i" is unsigned, the x*y must have been converted to signed.......


    I know it's being converted to a 32 bit int because sizeof = 4

    Can someone tell me what's going on?
    thanks!!

    p.s. i know that it's bad practice to compute in the condition
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The usual unary conversions are applied in turn to each operand in an expression. They serve to make the job of the compiler's expression evaluator easier by limiting the number of types that must be supported. One of these rules is:
    An unsigned type of rank less than int, all of whose values can be represented by type int is converted to int.
    The usual binary conversions are applied when an expression is evaluated. The usual binary conversions occur after the usual unary conversions and serve to convert all operands to a single common type, which is typically also the type of the result. One of the these rules is:
    If either operand has any signed type and the other operand has any signed type, then both are converted to the signed type with the greater rank.

    In your case, the usual unary conversions converts both x and y into ints. The usual binary conversions are satisfied to leave them as ints and it determines that the result will also be an int. Thus, your logical expression compares unsigned int i to the signed int result of the multiplication. The usual conversions have rules to handle this operation, but it is not uncommon to get a compiler warning to alert you that types whose domains do not overlap are being used.

    By the way, this is why you don't always get major improvements in execution time by using the smallest possible types. Your variables might be stored in chars or shorts, but your arithmetic still takes place with ints.
    Last edited by donbock; Jun 4 '11, 12:50 PM. Reason: Minor changes to improve clarity.

    Comment

    • dissectcode2
      New Member
      • Apr 2009
      • 32

      #3
      where can I find these rules? I searched the GNU C Library Reference Manual and GNU Compiler Collection manual and I can't find the rules you posted above in either one. thanks so much for helping!

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        I found them in chapter 6.3 (The Usual Conversions) in "C: A Reference Manual", 5th edition, Harbison and Steele, 2002, ISBN 0-13-089592X. You can also take a look at section 6.3 (Conversions) of the C99 specification.

        Comment

        Working...