Sign in to follow this  

Unsigned and Signed ints in C++

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this