What is -(-128) for signed single byte char in C?

后端 未结 3 1176
再見小時候
再見小時候 2021-01-18 06:45

My little program:

#include 

int main() {
    signed char c = -128;
    c = -c;
    printf(\"%d\", c);
    return 0;
}

prin

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 07:49

    The operand of the unary minus first undergoes standard promitions, so it is of type int, which can represent the value -128. The result of the operation is the value 128, also of type int. The conversion from int to signed char, being a narrowing of signed types, is implementation-defined.

    (Your implementation seems to do a simple wrap-around: 125, 126, 127, -128, -127, ...)

提交回复
热议问题