Warning : overflow in implicit constant conversion

Nawaz picture Nawaz · Feb 23, 2011 · Viewed 79.9k times · Source

In the following program, the line 5 does give overflow warning as expected, but surprisingly the line 4 doesn't give any warning in GCC: http://www.ideone.com/U0BXn

int main()
{
    int i = 256;
    char c1 = i;    //line 4
    char c2 = 256;  //line 5
    return 0;
}

I was thinking both lines should give overflow warning. Or is there something I'm missing?


The topic which led me to do this experiment is this: typedef type checking?

There I said the following(which I deleted from my answer, because when I run it, it didn't show up as I had expected):

//However, you'll get warning for this case:

typedef int  T1;
typedef char T2;

T1 x = 256;     
T2 y = x; //possible overflow warning! (but it doesn't give warning :()

Answer

Edward Strange picture Edward Strange · Feb 23, 2011

-Wall doesn't include many options. -Wconversion is one of them and warns about the behavior you're interested in.

See http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html