public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* unsigned
@ 2006-12-04 21:48 Trevis Rothwell
  2006-12-04 22:04 ` unsigned Erik
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Trevis Rothwell @ 2006-12-04 21:48 UTC (permalink / raw)
  To: gcc-help

Given the following program:

  int main()
  {
    unsigned int squiggy = 2147483648;
  }

Compliing on GCC 3.2.3, I get the following warning:

  $ gcc foo.c
  foo.c: In function `main':
  foo.c:3: warning: decimal constant is so large that it is unsigned

To the best of my knowledge, 2147483648 should fit with ample room to
spare in an unsigned (4-byte) integer.

What's going on?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: unsigned
  2006-12-04 21:48 unsigned Trevis Rothwell
@ 2006-12-04 22:04 ` Erik
  2006-12-04 22:17 ` unsigned Luke Powell
  2006-12-05 13:08 ` unsigned John Love-Jensen
  2 siblings, 0 replies; 6+ messages in thread
From: Erik @ 2006-12-04 22:04 UTC (permalink / raw)
  To: gcc-help

Trevis Rothwell skrev:
> Given the following program:
>
>  int main()
>  {
>    unsigned int squiggy = 2147483648;
>  }
>
> Compliing on GCC 3.2.3, I get the following warning:
>
>  $ gcc foo.c
>  foo.c: In function `main':
>  foo.c:3: warning: decimal constant is so large that it is unsigned
>
> To the best of my knowledge, 2147483648 should fit with ample room to
> spare in an unsigned (4-byte) integer.
>
> What's going on?
With gcc 4.1.1, I get this:

$ gcc foo.c
foo.c: In function 'main':
foo.c:3: warning: this decimal constant is unsigned only in ISO C90

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: unsigned
  2006-12-04 21:48 unsigned Trevis Rothwell
  2006-12-04 22:04 ` unsigned Erik
@ 2006-12-04 22:17 ` Luke Powell
  2006-12-04 22:41   ` unsigned Young, Michael
  2006-12-05 13:08 ` unsigned John Love-Jensen
  2 siblings, 1 reply; 6+ messages in thread
From: Luke Powell @ 2006-12-04 22:17 UTC (permalink / raw)
  To: Trevis Rothwell; +Cc: gcc-help

C tries to generate integers by default, so when you write that number,
which is larger than the signed integer boundary, it will instead
generate an unsigned integer instead and let you know about it. You
should be able to get rid of the warning by writing it as 2147483648U (U
for unsigned).

On 12/04/2006 03:48 PM, Trevis Rothwell wrote:
> Given the following program:
> 
>  int main()
>  {
>    unsigned int squiggy = 2147483648;
>  }
> 
> Compliing on GCC 3.2.3, I get the following warning:
> 
>  $ gcc foo.c
>  foo.c: In function `main':
>  foo.c:3: warning: decimal constant is so large that it is unsigned
> 
> To the best of my knowledge, 2147483648 should fit with ample room to
> spare in an unsigned (4-byte) integer.
> 
> What's going on?
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: unsigned
  2006-12-04 22:17 ` unsigned Luke Powell
@ 2006-12-04 22:41   ` Young, Michael
  0 siblings, 0 replies; 6+ messages in thread
From: Young, Michael @ 2006-12-04 22:41 UTC (permalink / raw)
  To: Luke Powell, Trevis Rothwell; +Cc: gcc-help

>C tries to generate integers by default, so when you write that number,
>which is larger than the signed integer boundary, it will instead
>generate an unsigned integer instead and let you know about it. You
>should be able to get rid of the warning by writing it as 2147483648U (U
>for unsigned).
>

Yes.  See C++ Std 2.13.1 for more info on this.  However, I'd like to point
out that the standard doesn't guarantee that 2147483648 is representable by
an integer or that an integer is 4 bytes in length - that is implementation
dependent (really, CPU/platform dependent - see 3.9.1).  On a 16-bit machine,
with a compiler that has 4 byte long integers, you may need to write "2147483648UL".
You didn't mention the platform, or the exact compiler you're using - I assumed
g++, but, from the extensions in the example below, maybe it was just the C compiler.

But I have a question - how does one write this portably (in C++)?

   typedef unsigned int uint_32_t ;  // similar to C99 types and common practice
   ...
   uint_32_t squiggy( 2147483648 ) ;  // Does this work w/o generating a warning?
   ...

if the above code still generates a warning, then does one resort to the following? :

   // Macro definition changes with platform, depending on length of integer
   #define UINT32_LITERAL( x ) xUL
   ...
   uint_32_t squiggy( UINT32_LITERAL( 2147483648 ) ) ;  
   ...
 
>On 12/04/2006 03:48 PM, Trevis Rothwell wrote:
>> Given the following program:
>> 
>>  int main()
>>  {
>>    unsigned int squiggy = 2147483648;
>>  }
>> 
>> Compliing on GCC 3.2.3, I get the following warning:
>> 
>>  $ gcc foo.c
>>  foo.c: In function `main':
>>  foo.c:3: warning: decimal constant is so large that it is unsigned
>> 
>> To the best of my knowledge, 2147483648 should fit with ample room to
>> spare in an unsigned (4-byte) integer.
>> 
>> What's going on?
>> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: unsigned
  2006-12-04 21:48 unsigned Trevis Rothwell
  2006-12-04 22:04 ` unsigned Erik
  2006-12-04 22:17 ` unsigned Luke Powell
@ 2006-12-05 13:08 ` John Love-Jensen
  2 siblings, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-12-05 13:08 UTC (permalink / raw)
  To: Trevis Rothwell, MSX to GCC

> What's going on?

Hi Trevis,

2147483648 is too big to fit in a signed int on your machine (32-bit signed
int).  So the compiler is treating it as 2147483648U, even though you did
not specify 2147483648U.

Note: 2147483648 is 0x80000000 which is one bigger than the largest 32-bit
signed int.  Not just "no room to spare", but one step beyond.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: unsigned
@ 2006-12-05 19:25 Kaz Kylheku
  0 siblings, 0 replies; 6+ messages in thread
From: Kaz Kylheku @ 2006-12-05 19:25 UTC (permalink / raw)
  To: Trevis Rothwell, gcc-help

 Trevis Rothwell wrote:
> Given the following program:
> 
>   int main()
>   {
>     unsigned int squiggy = 2147483648;
>   }
> 
> Compliing on GCC 3.2.3, I get the following warning:
> 
>   $ gcc foo.c
>   foo.c: In function `main':
>   foo.c:3: warning: decimal constant is so large that it is unsigned
> 
> To the best of my knowledge, 2147483648 should fit with ample room to
> spare in an unsigned (4-byte) integer.

Ample room to spare? How so? It requires all 32 bits.  There is ample
/range/ to spare, maybe. :)

> What's going on?

The warning has nothing to do with the unsigned declaration. The
compiler simply isn't smart enough to realize that you are assigning the
constant to a type which can hold its value. The diagnostic is just
about the constant, independently of how it is used.

In C90 (let's ignore C99 and long long), an integer constant which does
not have any suffix (U or L) is given a type according these rules:

- if its value fits into int, then it is int
- otherwise if its value fits into unsigned int, then that's the type,
- otherwise long,
- then unsigned long.

You have written a constant which doesn't fit into int and must be made
into unsigned int, and the compiler is warning about that, that is all.
Try 2147483648U.

In C99 the rules have changed. A decimal integer constant is assigned
the first of these types that can represent its value:

- int
- long
- long long

The unsigned types are not considered. So that explains the reasoning
behind the new warning found in gcc 4.1.1.

In C99, only octal and hexadecimal constants (without the U suffix) have
the behavior that their type can be chosen from among the signed and
unsigned types. An unsuffixed hex or octal constant is assigned the
first one from this list:

- int
- unsigned int
- long
- unsigned long
- long long
- unsigned long long

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-12-05 19:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-04 21:48 unsigned Trevis Rothwell
2006-12-04 22:04 ` unsigned Erik
2006-12-04 22:17 ` unsigned Luke Powell
2006-12-04 22:41   ` unsigned Young, Michael
2006-12-05 13:08 ` unsigned John Love-Jensen
2006-12-05 19:25 unsigned Kaz Kylheku

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).