Jump to content
  • Advertisement
Sign in to follow this  
zealotgi

Unsigned and Signed ints in C++

This topic is 4902 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!