Problem 5 of Quiz on Programming Fundamentals 1

Please clear the following for me:

  1. What happens when we store a signed integer in an unsigned type?
  2. Is it not necessary to mention ‘int’ when we are using data type modifiers like ‘unsigned’,‘long’ etc?

The output of this question depends upon the datamodifiers:

Let’s say you assign -2 to an unsigned integer, then for 32 bits int it will results in 2^32 - 2
It would be 2^16 - 2 for short unsigned integer
Similarly, for long long unsigned integer it would be 2^64 - 2
And for signed integer, it will be -2.

Yes, as an unsigned type cannot store the negative sign, thus the compiler converts it into an above mentioned unsigned representation.

Nope, it’s not a necessity to mention “int” as these datatype modifiers are for int only.

Hope, this would help.
Give a like, if you are satisfied.

1 Like