Signed to unsigned conversion in C - is it always safe?

前端 未结 8 1943
北荒
北荒 2020-11-22 01:55

Suppose I have the following C code.

unsigned int u = 1234;
int i = -5678;

unsigned int result = u + i;

What implicit conversions are goin

相关标签:
8条回答
  • 2020-11-22 02:51

    When one unsigned and one signed variable are added (or any binary operation) both are implicitly converted to unsigned, which would in this case result in a huge result.

    So it is safe in the sense of that the result might be huge and wrong, but it will never crash.

    0 讨论(0)
  • 2020-11-22 02:58

    Horrible Answers Galore

    Ozgur Ozcitak

    When you cast from signed to unsigned (and vice versa) the internal representation of the number does not change. What changes is how the compiler interprets the sign bit.

    This is completely wrong.

    Mats Fredriksson

    When one unsigned and one signed variable are added (or any binary operation) both are implicitly converted to unsigned, which would in this case result in a huge result.

    This is also wrong. Unsigned ints may be promoted to ints should they have equal precision due to padding bits in the unsigned type.

    smh

    Your addition operation causes the int to be converted to an unsigned int.

    Wrong. Maybe it does and maybe it doesn't.

    Conversion from unsigned int to signed int is implementation dependent. (But it probably works the way you expect on most platforms these days.)

    Wrong. It is either undefined behavior if it causes overflow or the value is preserved.

    Anonymous

    The value of i is converted to unsigned int ...

    Wrong. Depends on the precision of an int relative to an unsigned int.

    Taylor Price

    As was previously answered, you can cast back and forth between signed and unsigned without a problem.

    Wrong. Trying to store a value outside the range of a signed integer results in undefined behavior.

    Now I can finally answer the question.

    Should the precision of int be equal to unsigned int, u will be promoted to a signed int and you will get the value -4444 from the expression (u+i). Now, should u and i have other values, you may get overflow and undefined behavior but with those exact numbers you will get -4444 [1]. This value will have type int. But you are trying to store that value into an unsigned int so that will then be cast to an unsigned int and the value that result will end up having would be (UINT_MAX+1) - 4444.

    Should the precision of unsigned int be greater than that of an int, the signed int will be promoted to an unsigned int yielding the value (UINT_MAX+1) - 5678 which will be added to the other unsigned int 1234. Should u and i have other values, which make the expression fall outside the range {0..UINT_MAX} the value (UINT_MAX+1) will either be added or subtracted until the result DOES fall inside the range {0..UINT_MAX) and no undefined behavior will occur.

    What is precision?

    Integers have padding bits, sign bits, and value bits. Unsigned integers do not have a sign bit obviously. Unsigned char is further guaranteed to not have padding bits. The number of values bits an integer has is how much precision it has.

    [Gotchas]

    The macro sizeof macro alone cannot be used to determine precision of an integer if padding bits are present. And the size of a byte does not have to be an octet (eight bits) as defined by C99.

    [1] The overflow may occur at one of two points. Either before the addition (during promotion) - when you have an unsigned int which is too large to fit inside an int. The overflow may also occur after the addition even if the unsigned int was within the range of an int, after the addition the result may still overflow.

    | improve this answer | |
    0 讨论(0)
提交回复
热议问题