Is “char foo = 255” undefined behavior if char is signed?

后端 未结 3 1419
-上瘾入骨i
-上瘾入骨i 2020-12-13 13:15

The following doesn\'t give me any warning whatsoever when compiled with gcc 4.5.2 on x86 machine with Linux:

char foo = 255;

But when I us

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 13:30

    Signed integer overflow causes undefined behavior only when it occurs when evaluating intermediate results of arithmetic expressions, e.g. during binary mutiplication, unary decrement etc. Also, converting floating-point value to integer type causes undefined behavior if the value is out of range.

    An overflowing integral conversion to signed type does not cause undefined behavior. Instead it produces implementation-defined result (with the possibility of an implementation-defined signal being raised).

提交回复
热议问题