Thread: [ C ] <unsigned short int> and negative numbers.
just wondering, why accept negative numbers ? can assign negative number , print out no warnings or whatsoever.code:unsigned short int number; /* 0 .. 31,767 */
thought should reject it, @ least overflow .. ?
i'm not sure asking.
unsigned short can store values 0 65535 (2^16 - 1).
if assign negative value, -1, compiler not complain. sorry if upsets you.
when attempt print value, result positive number, because print-streamer interpreting value unsigned value.
try code:
p.s. reason why negative number interpreted positive value because when most-significant-bit set (as negative number), value not interpreted sign-bit unsigned values. has context signed value.code:#include <stdio.h> int main() { unsigned short value; (int = 5; > -5; --i) { value = i; printf("value = %u\n", value); } return 0; }
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [ C ] <unsigned short int> and negative numbers.
Ubuntu
Comments
Post a Comment