The size of an unsigned int in C is 0 to 65,535 or 0 to 4,294,967,295
.
I tested with the following codes in C:
unsigned int number = 0;
number -= 300;
printf("%d\n", number);
OUTPUT: -300
I remember that if the value of unsigned int
variable goes below 0, it should wrap around and I was expecting the output to be something like 4294966996
. However the output printed from the console is -300
.
I tested similar statements in C++, it does give me 4294966996
.
My question is: Why is the output -300
despite the fact that it is an unsigned int
?
PS: I've looked through several posts with similar title, but they are not addressing the same issue:
Because printf("%d\n", number);
will print a signed value, even if the parameter is unsigned. Use printf("%u\n", number);
for an unsigned value to be correctly printed.