Jack Klein <(E-Mail Removed)> writes:

> On Sat, 10 Nov 2007 11:49:47 +0000, Ben Bacarisse

> <(E-Mail Removed)> wrote in comp.lang.c:

>> kerravon <(E-Mail Removed)> writes:

>> > The following C program:

>> >

>> > int main(void)

>> > {

>> > int x = -2147483648;

>> >

>> > return (0);

>> > }
[...]

>>

>> Except C has no negative literals. Your initializer is an expression:

>> the unary negation operator is applied to a constant. That constant,

>> 2147483648, is indeed so large that it is unsigned (in C90). Negating

>> this large unsigned number provokes, technically, integer overflow.

>

> No, arithmetic on unsigned types never overflows. The result of

> negating an unsigned integer has a well-defined result, namely

> UINT_MAX - value.

>

> If that value is outside the range of a signed int and used to

> initialize one, the result is implementation-defined.
Interesting. This is a change from C90 to C99.

In C90, an unsuffixed decimal integer constant's type is the first of

int

unsigned int

long int

unsigned long int

in which it will fit.

In C99, the list is

int

long int

long long int

So in C99, unlike in C90, an unsuffixed decimal integer constant can

never be of an unsigned type.

So if INT_MAX and LONG_MAX are both 2147483647, then the constant

2147483648 is of type unsigned int in C90 and of type long long int in

C99. In C90:

int x = -2147483648;

converts the unsigned int value 2147483648 to int, which yields an

implementation-defined result. But in C99, it converts the long long

int value -2147483648 to type int, which is well defined.

--

Keith Thompson (The_Other_Keith)

http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>

Looking for software development work in the San Diego area.

"We must do something. This is something. Therefore, we must do this."

-- Antony Jay and Jonathan Lynn, "Yes Minister"