INT_MIN and compiler diagnostic

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • p_cricket_guy@yahoo.co.in

    INT_MIN and compiler diagnostic


    Please see this test program:

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <limits.h>
    4
    5 int main (void)
    6 {
    7 int y = -2147483648;
    8 int x = INT_MIN;
    9
    10 printf("INT_MAX = %d INT_MIN = %d\n", INT_MAX,
    INT_MIN);
    11 printf("x = %d y = %d\n", x, y);
    12
    13
    14 return EXIT_SUCCESS;
    15 }

    Output:
    INT_MAX = 2147483647 INT_MIN = -2147483648
    x = -2147483648 y = -2147483648

    When I compile this using gcc, I get a diagnostic on line 7:

    [pcg@mylinux test]$gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
    /tmp/x.c: In function `main':
    /tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90

    However, on my system INT_MIN is indeed -2147483648 as
    suggested by the output of above program.

    Is there any specific reason for this diagnostic [as per ANSI C] ?

    However, line 8, which is logically equivalent to line 7 does not
    produce any diagnostic. INT_MIN is defined in limits.h as:

    # define INT_MIN (-INT_MAX - 1)
    # define INT_MAX 2147483647

    Thanks,
    pcg

  • bytebro

    #2
    Re: INT_MIN and compiler diagnostic

    On 28 Feb, 10:07, p_cricket_...@y ahoo.co.in wrote:
    When I compile this using gcc, I get a diagnostic on line 7:
    >
    [pcg@mylinux test]$gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
    /tmp/x.c: In function `main':
    /tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90
    On mine (Sun sparc) using gcc with exactly the same flags I get

    x.c:7: warning: decimal constant is so large that it is unsigned

    The program output is:

    INT_MAX = 2147483647 INT_MIN = -2147483648
    x = -2147483648 y = -2147483648

    So amusingly, it tells me the constant is unsigned, then prints it
    signed.

    Where's a language lawyer when you need one?!

    Comment

    • Beej Jorgensen

      #3
      Re: INT_MIN and compiler diagnostic

      <p_cricket_guy@ yahoo.co.inwrot e:
      >/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90
      I think it was addressed in this thread:


      ># define INT_MIN (-INT_MAX - 1)
      ># define INT_MAX 2147483647
      This produces the warning:

      int y = -2147483648;

      whereas this does not:

      int y = (-2147483647 - 1);

      (Interestingly, though, I guess I expected the preprocessor to calculate
      the final answer before passing it to the compiler, and it doesn't.

      And now, it doesn't even make sense that the preprocessor would do it.
      I mean, the compiler has to do that kind of stuff anyway, right?

      So why did I have it in the back of my mind that the preprocessor
      sometimes did simple math? Some holdover from the olden days of not-so-
      optimal compilers?)

      Finally,

      int y = -0x80000000;

      does not produce a warning, though it is the same as -2147483648.
      Apparently the rules are different for hex constants than they are for
      decimal constants (c99 6.4.4.1p5).

      -Beej

      Comment

      • pete

        #4
        Re: INT_MIN and compiler diagnostic

        bytebro wrote:
        INT_MAX = 2147483647 INT_MIN = -2147483648
        x = -2147483648 y = -2147483648
        >
        So amusingly, it tells me the constant is unsigned, then prints it
        signed.
        >
        Where's a language lawyer when you need one?!
        If INT_MAX equals 2147483647,
        the the type of 2147483648 can't be type int, can it?

        --
        pete

        Comment

        • bytebro

          #5
          Re: INT_MIN and compiler diagnostic

          On 28 Feb, 12:34, pete <pfil...@mindsp ring.comwrote:
          bytebro wrote:
          INT_MAX = 2147483647 INT_MIN = -2147483648
          x = -2147483648 y = -2147483648
          >
          So amusingly, it tells me the constant is unsigned, then prints it
          signed.
          >
          Where's a language lawyer when you need one?!
          >
          If INT_MAX equals 2147483647,
          the the type of 2147483648 can't be type int, can it?
          Erm... it doesn't say "2147483648 ", it says "-2147483648", which is
          exactly equal to INT_MIN, and is therefore a valid int value. The
          warning is therefore misleading, no?


          Comment

          • Chris Dollin

            #6
            Re: INT_MIN and compiler diagnostic

            bytebro wrote:
            On 28 Feb, 12:34, pete <pfil...@mindsp ring.comwrote:
            >bytebro wrote:
            INT_MAX = 2147483647 INT_MIN = -2147483648
            x = -2147483648 y = -2147483648
            >>
            So amusingly, it tells me the constant is unsigned, then prints it
            signed.
            >>
            Where's a language lawyer when you need one?!
            >>
            >If INT_MAX equals 2147483647,
            >the the type of 2147483648 can't be type int, can it?
            >
            Erm... it doesn't say "2147483648 ", it says "-2147483648", which is
            exactly equal to INT_MIN, and is therefore a valid int value. The
            warning is therefore misleading, no?
            No.

            `-2147483648` isn't a literal constant. It's an expression, the
            negation of `2147483648`.

            --
            Chris "electric hedgehog" Dollin
            "People are part of the design. It's dangerous to forget that." /Star Cops/

            Comment

            • Dik T. Winter

              #7
              Re: INT_MIN and compiler diagnostic

              In article <1172668875.979 428.288060@8g20 00cwh.googlegro ups.com"bytebro " <keith.willis@a ah.co.ukwrites:
              On 28 Feb, 12:34, pete <pfil...@mindsp ring.comwrote:
              bytebro wrote:
              INT_MAX = 2147483647 INT_MIN = -2147483648
              x = -2147483648 y = -2147483648
              So amusingly, it tells me the constant is unsigned, then prints it
              signed.
              Where's a language lawyer when you need one?!
              If INT_MAX equals 2147483647,
              the the type of 2147483648 can't be type int, can it?
              >
              Erm... it doesn't say "2147483648 ", it says "-2147483648", which is
              exactly equal to INT_MIN, and is therefore a valid int value. The
              warning is therefore misleading, no?
              Does it tell you "-2147483648" is unsigned? Strange. It should
              tell you the constant is unsigned, and the constant is "2147483648 ".
              There are no negative constants in C.
              --
              dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
              home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

              Comment

              • Richard Heathfield

                #8
                Re: INT_MIN and compiler diagnostic

                Dik T. Winter said:

                <snip>
                There are no negative constants in C.
                "No" is a counter-example.

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: rjh at the above domain, - www.

                Comment

                • CBFalconer

                  #9
                  Re: INT_MIN and compiler diagnostic

                  Beej Jorgensen wrote:
                  >
                  .... snip ...
                  >
                  This produces the warning:
                  >
                  int y = -2147483648;
                  >
                  whereas this does not:
                  >
                  int y = (-2147483647 - 1);
                  >
                  .... snip ...
                  >
                  Finally,
                  >
                  int y = -0x80000000;
                  >
                  does not produce a warning, though it is the same as -2147483648.
                  Apparently the rules are different for hex constants than they are
                  for decimal constants (c99 6.4.4.1p5).
                  In the first case you are negating the int 2147483648, which has
                  already overflowed and caused un/implementation defined behaviour.
                  In the second, you are negating the unsigned int 0x80000000, which
                  follows the rules for unsigned ints, and does not overflow.

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net>


                  Comment

                  • bytebro

                    #10
                    Re: INT_MIN and compiler diagnostic

                    On 28 Feb, 13:20, Chris Dollin <chris.dol...@h p.comwrote:
                    bytebro wrote:
                    On 28 Feb, 12:34, pete <pfil...@mindsp ring.comwrote:
                    >
                    If INT_MAX equals 2147483647,
                    the the type of 2147483648 can't be type int, can it?
                    >
                    Erm... it doesn't say "2147483648 ", it says "-2147483648", which is
                    exactly equal to INT_MIN, and is therefore a valid int value. The
                    warning is therefore misleading, no?
                    >
                    No.
                    >
                    `-2147483648` isn't a literal constant. It's an expression, the
                    negation of `2147483648`.
                    Wow. I've been mucking around with C for about 20 years now, and
                    that's the first time I've come across that!

                    The thing is, in the OP's code lines 7 and 8, it says:

                    7 int y = -2147483648;
                    8 int x = INT_MIN;

                    which are _completely_ equivalent (INT_MIN is _defined_ as
                    -2147483648), and yet line 7 generates the warning:

                    /tmp/x.c:7: warning: this decimal constant is unsigned only in ISO
                    C90

                    on his system, and the warning:

                    x.c:7: warning: decimal constant is so large that it is unsigned

                    on my system, but line 8 generates no warning at all for either of us.

                    Comment

                    • Nelu

                      #11
                      Re: INT_MIN and compiler diagnostic

                      Richard Heathfield wrote:
                      Dik T. Winter said:
                      >
                      <snip>
                      >
                      >There are no negative constants in C.
                      >
                      "No" is a counter-example.
                      >
                      It's a string literal. Attempting to modify it may succeed (UB).
                      It will compile fine so it's not a good example, right? :-)

                      --
                      Ioan - Ciprian Tandau
                      tandau _at_ freeshell _dot_ org (hope it's not too late)
                      (... and that it still works...)

                      Comment

                      • Dik T. Winter

                        #12
                        Re: INT_MIN and compiler diagnostic

                        In article <1172676891.968 236.185190@s48g 2000cws.googleg roups.com"byteb ro" <keith.willis@a ah.co.ukwrites:
                        On 28 Feb, 13:20, Chris Dollin <chris.dol...@h p.comwrote:
                        ....
                        No.

                        `-2147483648` isn't a literal constant. It's an expression, the
                        negation of `2147483648`.
                        ....
                        The thing is, in the OP's code lines 7 and 8, it says:
                        >
                        7 int y = -2147483648;
                        8 int x = INT_MIN;
                        >
                        which are _completely_ equivalent (INT_MIN is _defined_ as
                        -2147483648), and yet line 7 generates the warning:
                        Please check it. It will probably defined as (- 2147483647 - 1),
                        which is something different.
                        --
                        dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                        home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                        Comment

                        • Richard Tobin

                          #13
                          Re: INT_MIN and compiler diagnostic

                          In article <1172676891.968 236.185190@s48g 2000cws.googleg roups.com>,
                          bytebro <keith.willis@a ah.co.ukwrote:
                          >The thing is, in the OP's code lines 7 and 8, it says:
                          >
                          7 int y = -2147483648;
                          8 int x = INT_MIN;
                          >
                          >which are _completely_ equivalent (INT_MIN is _defined_ as
                          >-2147483648)
                          According to the OP, on his system INT_MIN is defined as

                          # define INT_MIN (-INT_MAX - 1)

                          which produces the same result, but does not involve the literal 2147483648.
                          This seems to be a common trick; on my Mac it is defined as (-0x7fffffff - 1).

                          -- Richard
                          --
                          "Considerat ion shall be given to the need for as many as 32 characters
                          in some alphabets" - X3.4, 1963.

                          Comment

                          • strangerdream@gmail.com

                            #14
                            Re: INT_MIN and compiler diagnostic

                            On Feb 28, 7:14 pm, "Dik T. Winter" <Dik.Win...@cwi .nlwrote:
                            <snip>
                            There are no negative constants in C.
                            But the warning message says:
                            warning: this decimal constant is unsigned only in ISO C90

                            What is a signed constant then?




                            Comment

                            • Richard Tobin

                              #15
                              Re: INT_MIN and compiler diagnostic

                              In article <1172682091.061 155.235130@z35g 2000cwz.googleg roups.com>,
                              <strangerdream@ gmail.comwrote:
                              >There are no negative constants in C.
                              >
                              >But the warning message says:
                              >warning: this decimal constant is unsigned only in ISO C90
                              >
                              >What is a signed constant then?
                              The question is whether the constant has an unsigned type, or is
                              a positive value of a signed type.

                              -- Richard


                              --
                              "Considerat ion shall be given to the need for as many as 32 characters
                              in some alphabets" - X3.4, 1963.

                              Comment

                              Working...