473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is this standards compliant?

Aka, "How to make this warning go away?"

I have some code that contains:

#define FOO 2123456789L

I.e., a number that is greater than the limit for a signed 32 bit int.

I cannot use the above in a statement w/o compiler (gcc 2.95) issuing
a warning that says: decimal constant so large it must be unsigned.

For example:

unsigned int baz = FOO;

Should be OK, right?

Notes:
1) This is on a machine where ints and longs are both 32 bits.
2) I get same results with "long" instead of "int" (in the above
declaration).

Also, I tried:

#define FOO 2000000000L+123456789L;

And I get a different error (error this time) saying integer overflow...

I assume somewhere in the standard it says that compiletime math has to
work up to some level, right?

Nov 14 '05 #1
7 1346
ga*****@yin.interaccess.com (Kenny McCormack) writes:
I have some code that contains:

#define FOO 2123456789L

I.e., a number that is greater than the limit for a signed 32 bit int.

I cannot use the above in a statement w/o compiler (gcc 2.95) issuing
a warning that says: decimal constant so large it must be unsigned.


Tell the compiler that it is unsigned:
#define FOO 2123456789UL
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Nov 14 '05 #2
In article <87************@benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.edu> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
I have some code that contains:

#define FOO 2123456789L

I.e., a number that is greater than the limit for a signed 32 bit int.

I cannot use the above in a statement w/o compiler (gcc 2.95) issuing
a warning that says: decimal constant so large it must be unsigned.
Tell the compiler that it is unsigned:
#define FOO 2123456789UL


Good one. Thanks. I did not know about the U qualifier for constants.
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield


Cute. Luckily, not applicable to this thread (yet...)

Nov 14 '05 #3
Kenny McCormack wrote:
Also, I tried:

#define FOO 2000000000L+123456789L;

^^
Lose the spurious ';'. Parentheses are nice here:
#define FOO (2000000000L+123456789L)
Nov 14 '05 #4
Kenny McCormack wrote on 30/12/04 :
Aka, "How to make this warning go away?"

I have some code that contains:

#define FOO 2123456789L


should be

#define FOO 2123456789UL

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #5
Kenny McCormack wrote:
Aka, "How to make this warning go away?"

I have some code that contains:

#define FOO 2123456789L

I.e., a number that is greater than the limit for a signed 32 bit int.

The limit for a signed 32 bit int is 2147483647, so this
number is actually OK. (I guess it was some other value you
were working with..)
I assume somewhere in the standard it says that compiletime math has to work up to some level, right?


No. Also, the math happens at run-time. Because of the as-if rule,
the compiler can do it at compile-time, but this does not necessarily
turn UB into defined behaviour.

For 16 bit int:
int a = 33000; /* UB - out of range of int (I think) */
int a = 30000 + 3000; /* UB - signed int overflow */
long a = 30000u + 3000; /* OK */
unsigned short b = 30000 + 3000; /* UB - signed int overflow */
unsigned short b = (unsigned short)30000 + 3000; /* OK */

Nov 14 '05 #6
In article <33*************@individual.net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
Kenny McCormack wrote:
Also, I tried:

#define FOO 2000000000L+123456789L; ^^
Lose the spurious ';'.


typo (aka, transcription error).
Parentheses are nice here:
#define FOO (2000000000L+123456789L)


As someone pointed out, this isn't the actual number, so to get the desired
observed results you have to make the first digit of the second number
something greater than 1. Anyway, I tried many things with parens - none
of which helped. The trick, as someone else noted, is to put a U in there.

Thanks also to "Old Wolf" for the explanation re: the "as if" rule.

Nov 14 '05 #7
On Thu, 30 Dec 2004 18:44:22 GMT, Kenny McCormack
<ga*****@yin.interaccess.com> wrote:
Aka, "How to make this warning go away?"

I have some code that contains:

#define FOO 2123456789L

I.e., a number that is greater than the limit for a signed 32 bit int.

I cannot use the above in a statement w/o compiler (gcc 2.95) issuing
a warning that says: decimal constant so large it must be unsigned.
If you write it with UL suffix instead of just L the warning should go
away (and you'll get a correct warning if you try to assign it to a
signed long).
For example:

unsigned int baz = FOO;

Should be OK, right?
In some languages it would, because the compiler would start at the
assignment and propagate the type to the expression (Algol and Pascal, I
believe, do that). C takes the opposite approach, it assumes that you
know what you are doing with the expression and evaluates that, then
assigns it. So the steps go something like:

evaluate 2123456789, make sure it'a a long (the L suffix). This gives
your warning.

use it to initialise baz, this works fine.
Notes:
1) This is on a machine where ints and longs are both 32 bits.
2) I get same results with "long" instead of "int" (in the above
declaration).
Yes, you would, the error is in telling the compiler that 2123456789 is
a signed long (the L suffix).
Also, I tried:

#define FOO 2000000000L+123456789L;

And I get a different error (error this time) saying integer overflow...
Also correct, you've now added two signed values and got an answer which
won't fit into a signed long.
I assume somewhere in the standard it says that compiletime math has to
work up to some level, right?


It /works/, it gave you a warning not an error. It's telling you,
though, that you may have made a mistake somewhere. For instance, you
might have 2200000000 when you meant 220000000 (the latter fitting
happily into a signed 32 bit variable). If you want the number to be
unsigned, say so explicitly by writing 2123456789UL.

Chris C
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

162
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
3
by: CJM | last post by:
On company in our group is looking at getting their rather basic website redeveloped externally. In typical fashion, they have declared that this project it 'marketing-led' rather than 'IT-led',...
17
by: Ian | last post by:
Hi there, Can anybody tell me where I can find a standards documents like you have in c#. I am trying to write javascript and would like to know what standards are i.e. Where to put the...
2
by: John Doe | last post by:
I have a webpage which shows and hides divs as a method of navigation. It works great in the ever evil IE. It works not so great (read not at all) in any standards compliant browser. I haven't...
1
by: Jacob Friis Larsen | last post by:
Do you know of a menu that is indexable and standards compliant? I found this: http://www.htmldog.com/articles/suckerfish/dropdowns/example/ Thanks, Jacob
250
by: Sugapablo | last post by:
Just out of curiosity, while checking on a site I was working on, I decided to throw a couple of the web's most popular URLs into the W3C Markup Validator. Out of microsoft.com, google.com,...
7
by: Brandon J. Van Every | last post by:
I think I've finally figured out MS's C# strategy. C# is now an ECMA/ISO standard. However, that's only true of the current incarnation. The next version, Whidbey, is not. ...
9
by: Jason Gogela | last post by:
Does anyone out there know why I should care whether a <span> is nested in a <p> or vice versa? What is the bennafit of adhering to this standard? It seems to me that regardless of which way you...
15
by: Zhang Weiwu | last post by:
http://www.w3.org/MarkUp/2004/xhtml-faq provided a trick to serve xhtml webpage to IE as application/xml I used that trick and now every one of my xhtml webpage have following first 4 starting...
9
by: -Lost | last post by:
http://blogs.msdn.com/ie/archive/2007/12/19/internet-explorer-8-and- acid2-a-milestone.aspx Oh my! A somewhat standards compliant Internet Explorer? What about JavaScript? Not that it proves...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.