Warnings for explicit use of value of LONG_MIN 
Author Message
 Warnings for explicit use of value of LONG_MIN

If the value of LONG_MIN is -2147483648, I should be able to
assign -2147483648 to a long variable, no?  GCC (2.95.3-5) is
giving me the following warning for the two lines indicated in
the program below (note that I don't get the warning when I use
LONG_MIN):
    foo.c:15: warning: decimal constant is so large that it is unsigned
    foo.c:16: warning: decimal constant is so large that it is unsigned

I don't get this on another C compiler.  The program works
correctly despite the warning.  Am I doing something screwy
here (besides bad style), or is this just a GCC issue?

(In this case, LONG_MIN seems to be expanding to
(- 2147483647L  -1), as if the compiler just doesn't like
-2147483648.)

/Dan

/**************************************************************/
#include <stdio.h>
#include <limits.h>

#define MIN_L  -2147483648
#define MIN_L2 -2147483648L
#define SMALL  -2147483647

int main()
{
    long std_min = LONG_MIN;      /* no warning here       */
    long my_min  = MIN_L;         /* get warning here      */
    long my_min2 = MIN_L2;        /* get warning here, too */
    long small   = SMALL;         /* no warning here       */

    printf ("std_min %ld\n", std_min);
    printf ("my_min  %ld\n", my_min);
    printf ("my_min2 %ld\n", my_min2);
    printf ("small   %ld\n", small);

Quote:
}



Sat, 18 Sep 2004 05:31:49 GMT  
 Warnings for explicit use of value of LONG_MIN

Quote:

> If the value of LONG_MIN is -2147483648, I should be able to
> assign -2147483648 to a long variable, no?

That won't do what you think it will.  C does not have negative
integer constants.  Rather, all integer constants are positive,
but the unary minus operator can used to negate them.  So when
you write -2147483648 on an implementation with 32-bit ints, you
get an unsigned int constant, which is then negated, producing
what is also an unsigned int.  (Yes, in C it is valid to apply
the unary minus operator to unsigned types.)

Quote:
> GCC (2.95.3-5) is
> giving me the following warning for the two lines indicated in
> the program below (note that I don't get the warning when I use
> LONG_MIN):
>     foo.c:15: warning: decimal constant is so large that it is unsigned
>     foo.c:16: warning: decimal constant is so large that it is unsigned

Which makes perfect sense.

Quote:
> (In this case, LONG_MIN seems to be expanding to
> (- 2147483647L  -1), as if the compiler just doesn't like
> -2147483648.)

That should also make perfect sense given the explanation above.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


Sat, 18 Sep 2004 05:35:09 GMT  
 Warnings for explicit use of value of LONG_MIN

Quote:

> If the value of LONG_MIN is -2147483648, I should be able to
> assign -2147483648 to a long variable, no?

Correct.

Quote:
>                                            GCC (2.95.3-5) is
> giving me the following warning for the two lines indicated in
> the program below (note that I don't get the warning when I use
> LONG_MIN):
>     foo.c:15: warning: decimal constant is so large that it is unsigned
>     foo.c:16: warning: decimal constant is so large that it is unsigned

> I don't get this on another C compiler.  The program works
> correctly despite the warning.  Am I doing something screwy
> here (besides bad style), or is this just a GCC issue?

> (In this case, LONG_MIN seems to be expanding to
> (- 2147483647L  -1), as if the compiler just doesn't like
> -2147483648.)

Given this definition of LONG_MIN, the warning diagnostic is bogus.
The implicit types of the first literal, and the result, are both
long (on a twos-complement machine, of course). Had it been written
as -2147483648, however, the implicit type would be unsigned long,
and the warning MIGHT have some merit.

I've found a number of bugs of this nature among the various
instantiations of gcc.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



Sat, 18 Sep 2004 05:46:41 GMT  
 Warnings for explicit use of value of LONG_MIN

Quote:


> > (In this case, LONG_MIN seems to be expanding to
> > (- 2147483647L  -1), as if the compiler just doesn't like
> > -2147483648.)

> That should also make perfect sense given the explanation above.

I disagree. Both operands are of type long or smaller, therefore the
widening rules call for a result of type long. It's even representable,
so no overflow occurs.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



Sat, 18 Sep 2004 05:50:06 GMT  
 Warnings for explicit use of value of LONG_MIN

Quote:



> > > (In this case, LONG_MIN seems to be expanding to
> > > (- 2147483647L  -1), as if the compiler just doesn't like
> > > -2147483648.)

> > That should also make perfect sense given the explanation above.

> I disagree. Both operands are of type long or smaller, therefore the
> widening rules call for a result of type long. It's even representable,
> so no overflow occurs.

I think you must misunderstand what I'm saying.  I agree that
-2147483647L - 1 should not provoke a warning, and indeed it
doesn't, as the OP says.  My comment was meant to say that the
*reason* why LONG_MIN is expressed in that indirect fashion
should be perfectly clear given my explanation.
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Sat, 18 Sep 2004 06:04:17 GMT  
 Warnings for explicit use of value of LONG_MIN
Quote:




>> > > (In this case, LONG_MIN seems to be expanding to
>> > > (- 2147483647L  -1), as if the compiler just doesn't like
>> > > -2147483648.)

>> > That should also make perfect sense given the explanation above.

>> I disagree. Both operands are of type long or smaller, therefore the
>> widening rules call for a result of type long. It's even representable,
>> so no overflow occurs.

> I think you must misunderstand what I'm saying.  I agree that
> -2147483647L - 1 should not provoke a warning, and indeed it
> doesn't, as the OP says.  My comment was meant to say that the
> *reason* why LONG_MIN is expressed in that indirect fashion
> should be perfectly clear given my explanation.

[-]
http://gcc.gnu.org/ml/gcc-bugs/2001-10/msg00764.html - using
the hexadecimal representation so oughtn't cause a warning (I didn't
try),
Juergen

--
\ Real name     : Juergen Heinzl       \       no flames      /



Sat, 18 Sep 2004 06:45:40 GMT  
 Warnings for explicit use of value of LONG_MIN



Quote:

>> If the value of LONG_MIN is -2147483648, I should be able to
>> assign -2147483648 to a long variable, no?

> Correct.

>>                                            GCC (2.95.3-5) is
>> giving me the following warning for the two lines indicated in
>> the program below (note that I don't get the warning when I use
>> LONG_MIN):
>>     foo.c:15: warning: decimal constant is so large that it is unsigned
>>     foo.c:16: warning: decimal constant is so large that it is unsigned

>> I don't get this on another C compiler.  The program works
>> correctly despite the warning.  Am I doing something screwy
>> here (besides bad style), or is this just a GCC issue?

>> (In this case, LONG_MIN seems to be expanding to
>> (- 2147483647L  -1), as if the compiler just doesn't like
>> -2147483648.)

> Given this definition of LONG_MIN, the warning diagnostic is bogus.

Note the OP stated he *didn't* get the warning when he used LONG_MIN.

-Daniel



Sat, 18 Sep 2004 10:21:04 GMT  
 Warnings for explicit use of value of LONG_MIN
 [SNIP]

Quote:
> That won't do what you think it will.  C does not have negative
> integer constants.  Rather, all integer constants are positive,
> but the unary minus operator can used to negate them.

Is this declared in the standard, or is it just a common implementation?

Dan



Sat, 18 Sep 2004 21:30:14 GMT  
 Warnings for explicit use of value of LONG_MIN

Quote:


> > That won't do what you think it will.  C does not have negative
> > integer constants.  Rather, all integer constants are positive,
> > but the unary minus operator can used to negate them.

> Is this declared in the standard, or is it just a common implementation?

This is what the standard says.


Sun, 19 Sep 2004 03:03:09 GMT  
 Warnings for explicit use of value of LONG_MIN

Quote:



> > > That won't do what you think it will.  C does not have negative
> > > integer constants.  Rather, all integer constants are positive,
> > > but the unary minus operator can used to negate them.

> > Is this declared in the standard, or is it just a common implementation?

> This is what the standard says.

Thanks (learn something new every day)

Regards,
Dan



Sun, 19 Sep 2004 20:56:55 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Possible bug when using explicit fully qualitied names with namesapces

2. Serialization using explicit CArchive object.

3. Serialization problem using explicit CArchive object.

4. A happy medium between using-directives and explicit qualification

5. labs(LONG_MIN)

6. comparing an unsigned long with -LONG_MIN

7. Simple C++ question: Using return values (by value)

8. Test values & warnings

9. Values changing with no warning?!?!?!

10. function without return value: error or warning

11. VC++ 5.0: No Return Value warning in inline function

12. Warning Message (Re: Value, value, who's got the value?)

 

 
Powered by phpBB® Forum Software