Q:

What is a difference between unsigned int and signed int in C?

0

What is a difference between unsigned int and signed int in C?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

The signed and unsigned integer type has the same storage (according to the standard at least 16 bits) and alignment but still, there is a lot of difference them, in bellows lines, I am describing some difference between the signed and unsigned integer.

  • A signed integer can store the positive and negative value both but beside it unsigned integer can only store the positive value.
  • The range of nonnegative values of a signed integer type is a sub-range of the corresponding unsigned integer type.
    For example,
    Assuming the size of the integer is 2 bytes.
    signed int -32768 to +32767
    unsigned int 0 to 65535
  • When computing the unsigned integer, it never gets overflow because if the computation result is greater than the largest value of the unsigned integer type, it is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
    For example,
    Computational Result % (Largest value of the unsigned integer+1)
  • The overflow of the signed integer type is undefined.
  • If Data is signed type negative value, the right shifting operation of Data is implementation-dependent but for the unsigned type, it would be Data/ 2pos.
  • If Data is signed type negative value, the left shifting operation of Data shows the undefined behavior but for the unsigned type, it would be Data x 2pos.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the difference between a macro and a funct... >>
<< Are integers signed or unsigned?...