Skip to main content

Thread: [ C ] <unsigned short int> and negative numbers.


code:
unsigned short int number; /* 0 .. 31,767 */
just wondering, why accept negative numbers ? can assign negative number , print out no warnings or whatsoever.
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:
code:
#include <stdio.h>  int main() {    unsigned short value;     (int = 5; > -5; --i)    {       value = i;        printf("value = %u\n", value);    }     return 0; }
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.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [ C ] <unsigned short int> and negative numbers.


Ubuntu

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???