unsigned int vs signed int relevence in storing the value

Do unsigned int and signed int have any relevance as far as the storage is concerned. I know it has its relevance in a print statement i.e.; -1 will be treated as 4294967295 (%d and %u) . If we consider just storage of the value , does the unsigned or signed would make a difference?

1 answer

  • answered 2021-03-16 18:24 Eric Postpischil

    In C, you cannot have a value without a type. (Various operations are defined in terms of mathematical values, but each operation is specified to produce a result in a particular type, so, at each point in a C expression where there is a value, it has a type.) So any value is stored by storing the bytes of the object that represents it.

    The C 2018 standard specifies the representations of types in 6.2.6 and of integer types specifically in Objects are composed of one or more bits. Unsigned integers are represented with pure binary plus optional padding bits. The order of the bits is not specified. For a signed integer type, one of the bits is a sign bit, and each value bit has the same values as the same bit of the corresponding unsigned type. Some of the value bits in the unsigned type may be padding bits (unused for value) in the signed type. (But the total number of bits is the same, per 6.2.5 6.) The sign bit either indicates the value is negated or it represents the value −(2M) or −(2M−1), where M is the number of value bits. (Which of those three is implementation-defined.)

    Therefore, whether an integer type is signed or unsigned makes no difference regarding the interpretation of the common value bits. It affects only the interpretation of bits that are value bits in the unsigned type but a sign bit or padding bits in the signed type. (The latter are rare.)

    If the value in a signed integer type is the same as the value in its corresponding unsigned integer type, they have the same value in each of their common value bits and zeros in all of their unshared sign or value bits. (The padding bits are not specified by the C standard.)