warnings about large negative integer values

Hello,

Clang is producing curious warnings about certain large (in magnitude)
negative integer values.

I have a.c:

    #include <limits.h>
    #include <stdio.h>
    int main(void) {
        long int x = -9223372036854775808L;
        printf("%li\n", x);
        printf("%li\n", LONG_MIN);
        return 0;
    }

Running the compiled executable produces the two lines:

    -9223372036854775808
    -9223372036854775808

but compiling produced the warning:

    a.c:4:19: warning: integer constant is so large that it is unsigned
        long int x = -9223372036854775808L;
                      ^
    1 warning generated.

The preprocessor gives:

    ...
    # 3 "a.c" 2
    int main(void) {
        long int x = -9223372036854775808L;
        printf("%li\n", x);
        printf("%li\n", (-9223372036854775807L - 1L));
        return 0;
    }

So obviously the warning is incorrect. Is there a reason that warning
is produced here?

Mansour

For what it’s worth, gcc (4.3.2) is affected by the very same warning. It seems bogus and is probably due to the fact that the 9223372036854775808L would be too large for the type.

–Matthieu

Yes... that happens because "9223372036854775808L" doesn't fit into a
signed long; "-9223372036854775808L" does, but the standard states
that we aren't allowed to take that into account when we parse integer
literals.

The output happens to be the same for your testcase because
9223372036854775808UL happens to be the value you want, and it gets
implicitly converted to a signed integer; it is possible to write
testcases which would be affected by the difference.

-Eli

Ah, I get it now. I'll use the (-LONG_MAX-1) construct from now on.

Thanks for the info.

Mansour