1

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.

MciprianM
  • 513
  • 1
  • 7
  • 18

1 Answers1

2

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.
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Thanks, that's a great answer. Could you please add what warning flags did you use? Also could you please add what happens when you do an explicit cast like short x = (short) 2000000000; ? I'll edit my question to contain that case. Thanks! – MciprianM Jan 20 '14 at 10:54
  • `$ clang -c main.c` is the compile command. There are no warning flags. `(short) 2000000000` will provoke no implicit conversion warning because the conversion is then explicit: the cast means "Just do it". – Mike Kinghan Jan 21 '14 at 07:57