unsigned short;

Discussion in 'C Programming' started by Vijay Kumar R Zanvar, Feb 27, 2004.

  1. Hi clc,

    Please elaborate the warning:

    F:\Vijay\C> type unsigned2.c

    #include <stdio.h>
    #include <stdlib.h>

    int
    main ( void )
    {
    unsigned short i = 0;
    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


    Thank You.
    --
    Vijay Kumar R Zanvar
    Vijay Kumar R Zanvar, Feb 27, 2004
    #1
    1. Advertising

  2. Vijay Kumar R Zanvar <> scribbled the following:
    > Hi clc,


    > Please elaborate the warning:


    > F:\Vijay\C> type unsigned2.c


    > #include <stdio.h>
    > #include <stdlib.h>


    > int
    > main ( void )
    > {
    > unsigned short i = 0;
    > 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


    How do you expect an unsigned short to ever be less than -1?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You can pick your friends, you can pick your nose, but you can't pick your
    relatives."
    - MAD Magazine
    Joona I Palaste, Feb 27, 2004
    #2
    1. Advertising

  3. "Joona I Palaste" <> wrote in message
    news:c1mog8$dkc$...
    > Vijay Kumar R Zanvar <> scribbled the following:
    > > Hi clc,

    >
    > > Please elaborate the warning:

    >
    > > F:\Vijay\C> type unsigned2.c

    >
    > > #include <stdio.h>
    > > #include <stdlib.h>

    >
    > > int
    > > main ( void )
    > > {
    > > unsigned short i = 0;
    > > 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

    >
    > How do you expect an unsigned short to ever be less than -1?
    >


    I am confused here. Any promotions/conversions happen here?
    Can 6.3.1.3#2 not be applied?
    Vijay Kumar R Zanvar, Feb 27, 2004
    #3
  4. Vijay Kumar R Zanvar wrote:

    > Hi clc,
    >
    > Please elaborate the warning:
    >
    > F:\Vijay\C> type unsigned2.c
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int
    > main ( void )
    > {
    > unsigned short i = 0;
    > 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


    All unsigned integers are 0 or positive. They can never be less than a
    negative value, since they cannot be negative.
    Martin Ambuhl, Feb 27, 2004
    #4
  5. Vijay Kumar R Zanvar

    Nejat AYDIN Guest

    Vijay Kumar R Zanvar wrote:
    >
    > "Joona I Palaste" <> wrote in message
    > news:c1mog8$dkc$...
    > > Vijay Kumar R Zanvar <> scribbled the following:
    > > > Hi clc,

    > >
    > > > Please elaborate the warning:

    > >
    > > > F:\Vijay\C> type unsigned2.c

    > >
    > > > #include <stdio.h>
    > > > #include <stdlib.h>

    > >
    > > > int
    > > > main ( void )
    > > > {
    > > > unsigned short i = 0;
    > > > 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

    > >
    > > How do you expect an unsigned short to ever be less than -1?
    > >

    >
    > I am confused here. Any promotions/conversions happen here?
    > Can 6.3.1.3#2 not be applied?


    No, not here. But 6.3.1.8#1 applies:
    [...]
    Otherwise, 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.

    Since -1 is of type ``int'' and ``int'' can represent all of the values of the
    type of ``unsigned short'' (in your implementation), the variable ``i'' is
    converted to ``int''. So the expression ``i <= -1'' becomes always false
    because ``i'' is always non-negative.

    If all of the values of type ``unsigned short'' couldn't be represented by
    the type ``int'', then the variable ``i'' and ``-1'' would be converted to
    ``unsigned int'' and the expression would be always true.
    Nejat AYDIN, Feb 27, 2004
    #5
  6. "Vijay Kumar R Zanvar" <> writes:

    > "Joona I Palaste" <> wrote in message
    > news:c1mog8$dkc$...
    >> Vijay Kumar R Zanvar <> scribbled the following:
    >> > Hi clc,

    >>
    >> > Please elaborate the warning:

    >>
    >> > F:\Vijay\C> type unsigned2.c

    >>
    >> > #include <stdio.h>
    >> > #include <stdlib.h>

    >>
    >> > int
    >> > main ( void )
    >> > {
    >> > unsigned short i = 0;
    >> > 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

    >>
    >> How do you expect an unsigned short to ever be less than -1?

    >
    > I am confused here. Any promotions/conversions happen here?
    > Can 6.3.1.3#2 not be applied?


    `i' is promoted to either `int', or, if `int' cannot represent all
    values of `unsigned short', to `unsigned int'. Either way, the promotion
    doesn't change the value. Since it was nonnegative to start with, it is
    also nonnegative after the promotion.

    6.3.1.3#2 applies only if the value cannot be represented by new type,
    but in this case, it can. (Note the word "otherwise" in the standard
    text.)

    Martin


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
    Martin Dickopp, Feb 27, 2004
    #6
  7. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <c1mog8$dkc$> Joona I Palaste <> writes:

    >Vijay Kumar R Zanvar <> scribbled the following:
    >> Hi clc,

    >
    >> Please elaborate the warning:

    >
    >> F:\Vijay\C> type unsigned2.c

    >
    >> #include <stdio.h>
    >> #include <stdlib.h>

    >
    >> int
    >> main ( void )
    >> {
    >> unsigned short i = 0;
    >> 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

    >
    >How do you expect an unsigned short to ever be less than -1?


    It is a perfectly possible scenario. Can you figure out when?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Feb 27, 2004
    #7
  8. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <> Martin Dickopp <> writes:

    >"Vijay Kumar R Zanvar" <> writes:
    >
    >> "Joona I Palaste" <> wrote in message
    >> news:c1mog8$dkc$...
    >>> Vijay Kumar R Zanvar <> scribbled the following:
    >>> > Hi clc,
    >>>
    >>> > Please elaborate the warning:
    >>>
    >>> > F:\Vijay\C> type unsigned2.c
    >>>
    >>> > #include <stdio.h>
    >>> > #include <stdlib.h>
    >>>
    >>> > int
    >>> > main ( void )
    >>> > {
    >>> > unsigned short i = 0;
    >>> > 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
    >>>
    >>> How do you expect an unsigned short to ever be less than -1?

    >>
    >> I am confused here. Any promotions/conversions happen here?
    >> Can 6.3.1.3#2 not be applied?

    >
    >`i' is promoted to either `int', or, if `int' cannot represent all
    >values of `unsigned short', to `unsigned int'. Either way, the promotion
    >doesn't change the value. Since it was nonnegative to start with, it is
    >also nonnegative after the promotion.


    However, if i was promoted to unsigned int, then -1 was also converted
    to unsigned int, the result being UINT_MAX. In this case, i <= -1
    becomes a definite possibility.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Feb 27, 2004
    #8
  9. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <Z%B%b.23910$> Martin Ambuhl <> writes:

    >Vijay Kumar R Zanvar wrote:
    >
    >> Hi clc,
    >>
    >> Please elaborate the warning:
    >>
    >> F:\Vijay\C> type unsigned2.c
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> int
    >> main ( void )
    >> {
    >> unsigned short i = 0;
    >> 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

    >
    >All unsigned integers are 0 or positive. They can never be less than a

    ^^^^^^^^^^^^^^^^^^^^^
    >negative value, since they cannot be negative.


    C is not mathematics. Make the type of i unsigned int instead of unsigned
    short and i <= -1 *always* evaluates to true.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Feb 27, 2004
    #9
  10. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <> Nejat AYDIN <> writes:

    >Vijay Kumar R Zanvar wrote:
    >>
    >> I am confused here. Any promotions/conversions happen here?
    >> Can 6.3.1.3#2 not be applied?

    >
    >No, not here. But 6.3.1.8#1 applies:
    > [...]
    > Otherwise, 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.
    >
    >Since -1 is of type ``int'' and ``int'' can represent all of the values of the
    >type of ``unsigned short'' (in your implementation), the variable ``i'' is
    >converted to ``int''. So the expression ``i <= -1'' becomes always false
    >because ``i'' is always non-negative.


    Completely bogus analysis. It is the following text, from the same
    chapter and verse that applies here.

    Otherwise, the integer promotions are performed on both
    operands. Then the following rules are applied to the
    promoted operands: ^^^^^^
    ^^^^^^^^^^^^^^^^^
    If both operands have the same type, then no further
    conversion is needed.

    On his implementation, i becomes int after the integer promotions, so
    both operands have the same type.

    >If all of the values of type ``unsigned short'' couldn't be represented by
    >the type ``int'', then the variable ``i'' and ``-1'' would be converted to
    >``unsigned int'' and the expression would be always true.


    This is true, but the order of conversions would be: i becomes unsigned
    int after the integral promotions, while -1 remains signed int. So, the
    following paragraph applies:

    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.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Feb 27, 2004
    #10
  11. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <c1mmnf$1kbk4b$-berlin.de> "Vijay Kumar R Zanvar" <> writes:

    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >int
    >main ( void )
    >{
    > unsigned short i = 0;
    > 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


    You've got by now the correct answers. I only want to point out that
    the type unsigned short is probably the most "perverse" type from the C
    type system. Leave its usage to the expert C programmers.

    An error nobody mentioned was passing i to %u, although it's obvious that
    i gets promoted to signed int on your implementation, while %u expects
    an unsigned int.

    Furthermore, until you become an experienced C programmer, avoid as much
    as possible mixing signed and unsigned in the same expression. You'll
    avoid many "surprises" this way.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Feb 27, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    557
    Keith Thompson
    Jan 11, 2007
  2. Replies:
    4
    Views:
    817
    Kaz Kylheku
    Oct 17, 2006
  3. fancyerii
    Replies:
    21
    Views:
    1,483
    Roedy Green
    Nov 5, 2007
  4. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    673
    Eric Sosman
    Mar 5, 2008
  5. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page