It’s been a while since I start programming in C, however, I still feel confused about unsigned. If we compiled this code:
#include <stdio.h>
int main(int argc, char **argv)
{
unsigned int x = -1;
return 0;
}
both gcc and VC++ don’t raise any error or even a warning regarding using negative number with unsigned.
My question is that does unsigned do any internal job or it just a hint to the programer that this value shouldn’t be negative?
It is NOT just a hint. The following two snippets should behave differently:
Signed int:
Unsigned int:
And you could probably come up with 5 more examples where the signedness matters. For example, shifting right with the
>>operator.