unsigned short;

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vijay Kumar R Zanvar

    unsigned short;

    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


  • Joona I Palaste

    #2
    Re: unsigned short;

    Vijay Kumar R Zanvar <vijoeyz@hotpop .com> scribbled the following:[color=blue]
    > Hi clc,[/color]
    [color=blue]
    > Please elaborate the warning:[/color]
    [color=blue]
    > F:\Vijay\C> type unsigned2.c[/color]
    [color=blue]
    > #include <stdio.h>
    > #include <stdlib.h>[/color]
    [color=blue]
    > int
    > main ( void )
    > {
    > unsigned short i = 0;
    > for ( i = -10; i <= -1; i++ )
    > printf ("%u\n", i);
    > return EXIT_SUCCESS;
    > }[/color]
    [color=blue]
    > 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[/color]

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

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- 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

    Comment

    • Vijay Kumar R Zanvar

      #3
      Re: unsigned short;


      "Joona I Palaste" <palaste@cc.hel sinki.fi> wrote in message
      news:c1mog8$dkc $2@oravannahka. helsinki.fi...[color=blue]
      > Vijay Kumar R Zanvar <vijoeyz@hotpop .com> scribbled the following:[color=green]
      > > Hi clc,[/color]
      >[color=green]
      > > Please elaborate the warning:[/color]
      >[color=green]
      > > F:\Vijay\C> type unsigned2.c[/color]
      >[color=green]
      > > #include <stdio.h>
      > > #include <stdlib.h>[/color]
      >[color=green]
      > > int
      > > main ( void )
      > > {
      > > unsigned short i = 0;
      > > for ( i = -10; i <= -1; i++ )
      > > printf ("%u\n", i);
      > > return EXIT_SUCCESS;
      > > }[/color]
      >[color=green]
      > > F:\Vijay\C> gcc unsigned2.c -Wall
      > > unsigned2.c: In function `main':
      > > unsigned2.c:10: warning: comparison is always false due to limited range[/color][/color]
      of[color=blue][color=green]
      > > data type[/color]
      >
      > How do you expect an unsigned short to ever be less than -1?
      >[/color]

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


      Comment

      • Martin Ambuhl

        #4
        Re: unsigned short;

        Vijay Kumar R Zanvar wrote:
        [color=blue]
        > 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[/color]

        All unsigned integers are 0 or positive. They can never be less than a
        negative value, since they cannot be negative.


        Comment

        • Nejat AYDIN

          #5
          Re: unsigned short;

          Vijay Kumar R Zanvar wrote:[color=blue]
          >
          > "Joona I Palaste" <palaste@cc.hel sinki.fi> wrote in message
          > news:c1mog8$dkc $2@oravannahka. helsinki.fi...[color=green]
          > > Vijay Kumar R Zanvar <vijoeyz@hotpop .com> scribbled the following:[color=darkred]
          > > > Hi clc,[/color]
          > >[color=darkred]
          > > > Please elaborate the warning:[/color]
          > >[color=darkred]
          > > > F:\Vijay\C> type unsigned2.c[/color]
          > >[color=darkred]
          > > > #include <stdio.h>
          > > > #include <stdlib.h>[/color]
          > >[color=darkred]
          > > > int
          > > > main ( void )
          > > > {
          > > > unsigned short i = 0;
          > > > for ( i = -10; i <= -1; i++ )
          > > > printf ("%u\n", i);
          > > > return EXIT_SUCCESS;
          > > > }[/color]
          > >[color=darkred]
          > > > F:\Vijay\C> gcc unsigned2.c -Wall
          > > > unsigned2.c: In function `main':
          > > > unsigned2.c:10: warning: comparison is always false due to limited range[/color][/color]
          > of[color=green][color=darkred]
          > > > data type[/color]
          > >
          > > How do you expect an unsigned short to ever be less than -1?
          > >[/color]
          >
          > I am confused here. Any promotions/conversions happen here?
          > Can 6.3.1.3#2 not be applied?[/color]

          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.

          Comment

          • Martin Dickopp

            #6
            Re: unsigned short;

            "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
            [color=blue]
            > "Joona I Palaste" <palaste@cc.hel sinki.fi> wrote in message
            > news:c1mog8$dkc $2@oravannahka. helsinki.fi...[color=green]
            >> Vijay Kumar R Zanvar <vijoeyz@hotpop .com> scribbled the following:[color=darkred]
            >> > Hi clc,[/color]
            >>[color=darkred]
            >> > Please elaborate the warning:[/color]
            >>[color=darkred]
            >> > F:\Vijay\C> type unsigned2.c[/color]
            >>[color=darkred]
            >> > #include <stdio.h>
            >> > #include <stdlib.h>[/color]
            >>[color=darkred]
            >> > int
            >> > main ( void )
            >> > {
            >> > unsigned short i = 0;
            >> > for ( i = -10; i <= -1; i++ )
            >> > printf ("%u\n", i);
            >> > return EXIT_SUCCESS;
            >> > }[/color]
            >>[color=darkred]
            >> > 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[/color]
            >>
            >> How do you expect an unsigned short to ever be less than -1?[/color]
            >
            > I am confused here. Any promotions/conversions happen here?
            > Can 6.3.1.3#2 not be applied?[/color]

            `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. \_/

            Comment

            • Dan Pop

              #7
              Re: unsigned short;

              In <cunk728c301.fs f@zero-based.org> Martin Dickopp <expires-2004-03-31@zero-based.org> writes:
              [color=blue]
              >"Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
              >[color=green]
              >> "Joona I Palaste" <palaste@cc.hel sinki.fi> wrote in message
              >> news:c1mog8$dkc $2@oravannahka. helsinki.fi...[color=darkred]
              >>> Vijay Kumar R Zanvar <vijoeyz@hotpop .com> 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?[/color]
              >>
              >> I am confused here. Any promotions/conversions happen here?
              >> Can 6.3.1.3#2 not be applied?[/color]
              >
              >`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.[/color]

              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@ifh.de

              Comment

              • Dan Pop

                #8
                Re: unsigned short;

                In <c1mog8$dkc$2@o ravannahka.hels inki.fi> Joona I Palaste <palaste@cc.hel sinki.fi> writes:
                [color=blue]
                >Vijay Kumar R Zanvar <vijoeyz@hotpop .com> scribbled the following:[color=green]
                >> Hi clc,[/color]
                >[color=green]
                >> Please elaborate the warning:[/color]
                >[color=green]
                >> F:\Vijay\C> type unsigned2.c[/color]
                >[color=green]
                >> #include <stdio.h>
                >> #include <stdlib.h>[/color]
                >[color=green]
                >> int
                >> main ( void )
                >> {
                >> unsigned short i = 0;
                >> for ( i = -10; i <= -1; i++ )
                >> printf ("%u\n", i);
                >> return EXIT_SUCCESS;
                >> }[/color]
                >[color=green]
                >> 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[/color]
                >
                >How do you expect an unsigned short to ever be less than -1?[/color]

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

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: unsigned short;

                  In <Z%B%b.23910$hm 4.8122@newsread 3.news.atl.eart hlink.net> Martin Ambuhl <mambuhl@earthl ink.net> writes:
                  [color=blue]
                  >Vijay Kumar R Zanvar wrote:
                  >[color=green]
                  >> 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[/color]
                  >
                  >All unsigned integers are 0 or positive. They can never be less than a[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^[color=blue]
                  >negative value, since they cannot be negative.[/color]

                  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@ifh.de

                  Comment

                  • Dan Pop

                    #10
                    Re: unsigned short;

                    In <403EF27B.B5E5D C65@superonline .com> Nejat AYDIN <nejataydin@sup eronline.com> writes:
                    [color=blue]
                    >Vijay Kumar R Zanvar wrote:[color=green]
                    >>
                    >> I am confused here. Any promotions/conversions happen here?
                    >> Can 6.3.1.3#2 not be applied?[/color]
                    >
                    >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.[/color]

                    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.
                    [color=blue]
                    >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.[/color]

                    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@ifh.de

                    Comment

                    • Dan Pop

                      #11
                      Re: unsigned short;

                      In <c1mmnf$1kbk4b$ 1@ID-203837.news.uni-berlin.de> "Vijay Kumar R Zanvar" <vijoeyz@hotpop .com> writes:
                      [color=blue]
                      >#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[/color]

                      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@ifh.de

                      Comment

                      Working...