----- Original Message ----- 
From: "anand hd" 
To: 
Sent: Thursday, February 26, 2004 12:56 PM
Subject: [UTTARA] Re: signed or unsigned.

> Hi,
> For unsigned short, pls.. explain why we get the below warning.
> Regards,
> Anand
> 
> > And when unsigned int is changed to unsigned short
> > nothing is printed.
> > why?
> >
>     F:\Vijay\C> type unsigned2.c
>     #include 
>     #include 
>     int
>     main ( void )
>     {
>         unsigned short i ;
>         for ( i = -10; i <= -1; i++ )
>             printf ("%u\n", i);
>         return EXIT_SUCCESS;
>     }
>
>     F:\Vijay\C> gcc unsigned2.c -Wall
>     unsigned2.c: In function `main':
>     unsigned2.c:10: warning: comparison is always false due to limited range of data type
> 

The above program can be rewritten as follows.  This has been done just for 
easiness while explaining.

 1  #include 
 2  #include 
 3  #include 
 4
 5  int
 6  main ( void )
 7  {
 8      unsigned short i ;
 9
10      /* for ( i = -10; i <= -1; i++ )
11          printf ("%u\n", i);*/
12
13      i = -10;
14      while ( i <= -1 )
15      {
16          printf ( "%u\n", i );
17          i++;
18      }
19
20      printf ( "%u\n", i );
21      printf ( "%u\n", (USHRT_MAX+1) + (-10) );
22
23      return EXIT_SUCCESS;
24  }

Rules:

    [1]
    If the type of the operand with signed integer type can represent
    all of the values of the type of the operand with unsigned integer type, 
    then the operand with unsigned integer type is converted to the type of 
    the operand with signed integer type.

    [2]
    When a value with integer type is converted to an unsigned type, 
    and if the value is outside the range of unsigned type, 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.  

Note the following points:

    *   Because of Rule [2], the value of "i" (line 13) is

            i = (USHRT_MAX+1) + (-10)

        which is a large positive number.

    *   Rule [1] is interesting, and read it carefully.  For your compiler, as 
        well as mine, the sizeof (int) == 4 and sizeof (short) == 2.  The 
        following discussion applies to the line 14.  Since, -1 is of type 
        "int", and as "int" can represent all the values of "unsigned short", 
        "i" is converted/promoted to "int".  But, remember that the value of "i"
        has already been determined before the type conversion takes place.  
        Hence, "i" is a big positive integer failing the condition of line 14.

    *   Compilers are free to choose appropriate sizes of data type according
        to underlying hardware.  If sizeof (int) == sizeof (short), then 
        both, "i" and -1, will be promoted to "unsigned int".  In such case,
        the condition of line 14 becomes true.

        Read the section 2.2, Data Types and Sizes, of K&R II.

    Source: geocities.com/vijoeyz/faq/c

               ( geocities.com/vijoeyz/faq)                   ( geocities.com/vijoeyz)