Unsigned and Signed ints in C++
Okay, I assign a variable unsigned and it keeps comming up as a positive
Well heres the code:
// Assuming m_Font is already declared in DirectX
short int m = -2;
sprintf(varM, "Variable M: %lu ", m);
m_Font.Print(varM,2,2,400,100);
OUTPUT:
Variable M: 65534
It should be a negative number... can anyone tell me how to make it negative?
Unsigned integers cannot represent negative numbers. You need to use a default (signed) integer.
Quote:Original post by zealotgi
Okay, I assign a variable unsigned and it keeps comming up as a positive
That's what "unsigned" means, no sign.
Quote:
Well heres the code:
// Assuming m_Font is already declared in DirectX
short int m = -2;
sprintf(varM, "Variable M: %lu ", m);
m_Font.Print(varM,2,2,400,100);
OUTPUT:
Variable M: 65534
It should be a negative number... can anyone tell me how to make it negative?
Two problems here, both with the "%lu". First, the 'u' means unsigned. Use 'd' instead. Second, the 'l' means long, but you gave it a short (which is promoted to an int on the stack, so the 'd' is still ok). Since sprintf is expecting a long and all you have is an int, it could cause problems depending on the size of an int on your system.
At any rate, change the "Variable M: %lu " to "Variable M: %d " and you should get what you expected.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement