質問

Consider the following snippets:

short x = 2000000000;

short x = (short)2000000000;

int x = 1000000000 * 1000000000;

Can we get an warning(/error) for these in Clang? How? Starting with what version?

Thanks, Ciprian.

役に立ちましたか?

解決

As of clang 3.3, at least, you get warnings in both cases without even trying:

/* main.c */
short x = 2000000000;
int y = 1000000000 * 1000000000;

int main()
{
    return 0;
}

Compile:

$ clang -c main.c
main.c:1:11: warning: implicit conversion from 'int' to 'short' changes value
      from 2000000000 to -27648 [-Wconstant-conversion]
short x = 2000000000;
      ~   ^~~~~~~~~~
main.c:2:20: warning: overflow in expression; result is -1486618624 with type
      'int' [-Winteger-overflow]
int y = 1000000000 * 1000000000;
                   ^
2 warnings generated.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top