Oct 17, 2008 14:55
I just fixed a bug at work that formed an interesting puzzle. We have a quick printf function at work that handles formatting certain data types faster than microsoft printf (which is really slow). This code:
printf("%d", -2147483648)
produced this result:
-(
See if you can figure out why!
Leave a comment
Comments 5
(The comment has been removed)
Reply
The printer sees the negative bit, so it prints a '-' and then negatizes the number.
Being -2^31, this doesn't go well. Now besides being the wrong number, it's still negative.
So when it's time to calculate the ASCII code of the first digit by adding it to ASCII '0', subtraction is performed instead, going to ASCII '('.
Then it divides by 10, checks if it's still > 0 (oops, nope, it's negative!) and stops printing.
Reply
Reply
The other satisfactory answer is "because -( looks like a sad cyclops"
Reply
Reply
Leave a comment