Hacker News new | ask | show | jobs
by free_bip 877 days ago
The -1 here is hiding the fact that -1 is really just 0xffffffff due to two's complement (architecture dependant of course)

And printing it as %d is technically a misuse of printf, since %d means print as a signed integer. If you did %u (print as unsigned integer) instead you'd see the value is really 4,294,967,295 (again, platform dependant)

1 comments

I knew someone would say this :) But my point is that how can I assign a negative number to an unsigned int in the first place?
I would argue that you're not. But I use python in my day job
Hah yes, me too. Python has the opposite problem. C changes the type of a supplied value to fit the declared variable type. Python changes the type of the variable to fit its value's (or reference's) type.

So in C if I assign a char literal to an int, I get an int. If I do the same in Python, I get a string.

In a strongly typed language, which I'm defining as "needs the left and right hand side of an assignment expression to match somehow", I'd get a type error.