As the title suggests, I am curious about how an unsigned int
(or things like NSUInteger
, u_int_blah
, but I assume these are all typedef
s of the same thing) works. For example, when their value drops below zero, is an exeption raised? Will an error occur? One specific example of this would be indirectly setting the value to a negative number.
for (unsigned int x = 5; x > -10; x--) {
// will x ever reach below zero, or will the loop terminate
}
Also, another way to indirectly set it would be to have the user input it.
printf("Enter a number");
unsigned int x;
scanf("%ud", &x); // user enters something like -29
So really, I have three questions. What stops and unsigned int from being assigned to a negative number (unsigned int x = - 3
). How is this behavior implemented (by the compiler, or by other means). What happens when an unsigned int is assigned (directly or indirectly) to a negative value. Is the data corrupted? Does it overflow?
Thankyou
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…