473,765 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Feb 28 '07 #1
41 3017
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?!

Feb 28 '07 #2
<p_***********@ 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:

http://tinyurl.com/ytuxdv
># 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

Feb 28 '07 #3
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
Feb 28 '07 #4
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?
Feb 28 '07 #5
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/

Feb 28 '07 #6
In article <11************ **********@8g20 00cwh.googlegro ups.com"bytebro " <ke**********@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/
Feb 28 '07 #7
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
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 28 '07 #8
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>
Feb 28 '07 #9
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.

Feb 28 '07 #10

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

Similar topics

21
1958
by: JKop | last post by:
Today I wrote a function that returned an "std::ostringstream" by value. I compiled it with G++. It was throwing exceptions at run-time and closing. So I look through the code, and I look and I look... I forgot to put in the return statement in the aforementioned funtion, ie. it had no return statement.
20
2730
by: News | last post by:
I'm new to c and gcc. I was wondering if anyone can point me to a web page or book that has a list of the warning messages and their meanings. Are the warnings the same no matter what compiler you use, or are they different for each compiler? I have looked all over the gcc web site, but I can't find anything that explains what the warnings are. If someone could take the time to help me out and direct me to some info about this, I'd...
6
2722
by: M Welinder | last post by:
The title more or less says it all: in C99, is the value of INT_MIN % -1 well defined (when performed as signed integers) under the assumption of two-complement representation. Note, that this is not the usual negative-values-and-% question -- the problem here is that the corresponding signed division, INT_MIN / -1, overflows. Thus I don't see what use a%b = a-(a/b)*b can be here.
87
3368
by: rufus | last post by:
Is there a C-compiler (and for that matter C++ compiler) for windows that can be run from the commmand line?
30
4921
by: viza | last post by:
Hi all int i= INT_MIN; unsigned int u= -i; Is u guaranteed to have the absolute value of INT_MIN? Why it might not: -i has type (int), and -INT_MIN might be more than INT_MAX.
159
7110
by: bernard | last post by:
howdy! please recommend a good c compiler. - should be small - should be fast - should come with a good ide - should be inexpensive i am using windows os.
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.