473,786 Members | 2,615 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
41 3021
Richard Heathfield wrote:
Keith Thompson said:

<snip>
For example this program:

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 'a');
return 0;
}

is not strictly conforming because its output depends on
implementation-defined behavior, but a conforming hosted
implementation must accept it.

Why?
4p3: "A program that is correct in all other aspects, operating on
correct data, containing unspeciï¬ed behavior shall be a correct
program and act in accordance with 5.1.2.3."

And implementation-defined behaviour is a special case of unspecified
behaviour.

Mar 1 '07 #31
Richard Heathfield <rj*@see.sig.in validwrites:
Keith Thompson said:
<snip>
>For example this program:

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 'a');
return 0;
}

is not strictly conforming because its output depends on
implementati on-defined behavior, but a conforming hosted
implementati on must accept it.

Why?
Because.

A more specific question will yield a more specific answer. 8-)}

Seriously, are you suggesting that a conforming hosted implementation
is allowed to reject the above, but is not allowed to reject a similar
strictly conforming program such as the following?

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 97);
return 0;
}

(The output is factually incorrect on non-ASCII systems, but of course
that's irrelevant to strict conformance.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 1 '07 #32
Clark Cox said:
On 2007-03-01 08:47:26 -0800, Richard Heathfield <rj*@see.sig.in valid>
said:
>Keith Thompson said:

<snip>
>>For example this program:

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 'a');
return 0;
}

is not strictly conforming because its output depends on
implementatio n-defined behavior, but a conforming hosted
implementatio n must accept it.

Why?

Why wouldn't it have to accept it?
The Standard says:

"A conforming hosted implementation shall accept any strictly conforming
program."

On that basis, I consider it a requirement upon conforming hosted
implementations that they shall accept any strictly conforming program.
I can see no such requirement being imposed on conforming hosted
implementations with regard to programs that are not strictly
conforming. Can you?

>
'a' is a prefectly valid integer value, suitable for passing to printf
through the %d specifier. The program will produce different output
depending on the exact value of 'a', but I cannot imagine any reason
that a compiler could reject it outright
I can, but then I have a pretty good imagination. Can you *prove* that a
compiler *must not* reject it outright, with reference to the Standard?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 1 '07 #33
Harald van D?k said:
Richard Heathfield wrote:
>Keith Thompson said:

<snip>
For example this program:

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 'a');
return 0;
}

is not strictly conforming because its output depends on
implementation-defined behavior, but a conforming hosted
implementation must accept it.

Why?

4p3: "A program that is correct in all other aspects, operating on
correct data, containing unspeci[fi]ed behavior shall be a correct
program and act in accordance with 5.1.2.3."

And implementation-defined behaviour is a special case of unspecified
behaviour.
Where does the Standard require conforming hosted (or indeed
freestanding!) implementations to accept correct programs that are not
strictly conforming?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 1 '07 #34
Keith Thompson said:

<snip>
Seriously, are you suggesting that a conforming hosted implementation
is allowed to reject the above, but is not allowed to reject a similar
strictly conforming program such as the following?

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 97);
return 0;
}
No, I'm suggesting that I can't find any requirement on a conforming
implementation (of whatever flavour - either hosted or freestanding) to
accept programs that are not strictly conforming. Perhaps such a
requirement exists and I simply haven't discovered it yet. Can you
enlighten me as to where in the Standard this requirement is
documented?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 1 '07 #35
Richard Heathfield wrote:
Harald van D?k said:
Richard Heathfield wrote:
Keith Thompson said:

<snip>

For example this program:

#include <stdio.h>
int main(void)
{
printf("'a' = %d\n", 'a');
return 0;
}

is not strictly conforming because its output depends on
implementation-defined behavior, but a conforming hosted
implementation must accept it.

Why?
4p3: "A program that is correct in all other aspects, operating on
correct data, containing unspeci[fi]ed behavior shall be a correct
program and act in accordance with 5.1.2.3."

And implementation-defined behaviour is a special case of unspecified
behaviour.

Where does the Standard require conforming hosted (or indeed
freestanding!) implementations to accept correct programs that are not
strictly conforming?
In the paragraph I quoted. If the implementation doesn't accept the
correct program, it cannot act in accordance with 5.1.2.3.

See also 4p1. "In this International Standard, "shall" is to be
interpreted as a requirement on an implementation or on a program;
[...]" 4p3 contains a "shall", cannot be a requirement on a program,
so is a requirement on an implementation.

Mar 1 '07 #36
Harald van D?k said:
Richard Heathfield wrote:
<snip>
>Where does the Standard require conforming hosted (or indeed
freestanding !) implementations to accept correct programs that are
not strictly conforming?

In the paragraph I quoted. If the implementation doesn't accept the
correct program, it cannot act in accordance with 5.1.2.3.
You have the makings of a point, but I am not yet convinced. Can you
make your reasoning clearer, please?
See also 4p1. "In this International Standard, "shall" is to be
interpreted as a requirement on an implementation or on a program;
[...]" 4p3 contains a "shall", cannot be a requirement on a program,
so is a requirement on an implementation.
This assumes that implementations are required to accept correct
programs, and I have yet to see any unequivocal evidence of this
requirement. I hope you're right, but I can't yet see *why* you're
right.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 1 '07 #37
Richard Heathfield <rj*@see.sig.in validwrites:
Harald van D?k said:
>Richard Heathfield wrote:

<snip>
>>Where does the Standard require conforming hosted (or indeed
freestanding! ) implementations to accept correct programs that are
not strictly conforming?

In the paragraph I quoted. If the implementation doesn't accept the
correct program, it cannot act in accordance with 5.1.2.3.

You have the makings of a point, but I am not yet convinced. Can you
make your reasoning clearer, please?
>See also 4p1. "In this International Standard, "shall" is to be
interpreted as a requirement on an implementation or on a program;
[...]" 4p3 contains a "shall", cannot be a requirement on a program,
so is a requirement on an implementation.

This assumes that implementations are required to accept correct
programs, and I have yet to see any unequivocal evidence of this
requirement. I hope you're right, but I can't yet see *why* you're
right.
I assume there's something unclear here, but I'm not clear on what it
is.

C99 4p3:

A program that is correct in all other aspects, operating on
correct data, containing unspecified behavior shall be a correct
program and act in accordance with 5.1.2.3.

(5.1.2.3 is "Program execution".)

If the implementation rejects a "correct program", that program cannot
act in accordance with 5.1.2.3. A "correct program" must act in
accordance with 5.1.2.3. Therefore, a conforming implementation must
not reject a "correct program".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 1 '07 #38
Keith Thompson said:

<snip>
If the implementation rejects a "correct program", that program cannot
act in accordance with 5.1.2.3. A "correct program" must act in
accordance with 5.1.2.3. Therefore, a conforming implementation must
not reject a "correct program".
I'm inclined to agree, but I would not like to have to defend such a
position myself.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 2 '07 #39
Dik T. Winter wrote:
>
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.
Nit-picking, angels-on-pinhead-counting, pedant-delighting
counter-example:

enum { MINUS = -1, ZERO, PLUS };

Now `MINUS' is a constant with a negative value.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 2 '07 #40

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
2732
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
2723
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
3378
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
4927
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
7130
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
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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
10110
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
8989
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
7512
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
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
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
2894
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.