<p>When is it appropriate to use an unsigned variable over a signed one? What about in a <code>for</code> loop?</p>

<p>I hear a lot of opinions about this and I wanted to see if there was anything resembling a consensus. </p>

<pre><code>for (unsigned int i = 0; i &lt; someThing.length(); i++) {  
    SomeThing var = someThing.at(i);  
    // You get the idea.  
}
</code></pre>

<p>I know Java doesn't have unsigned values, and that must have been a concious decision on <a href="https://en.wikipedia.org/wiki/Sun_Microsystems" rel="noreferrer">Sun Microsystems</a>' part. </p>

[{'answer_id': 339, 'author': 'saint_groceon', 'author_id': 59, 'author_profile': 'https://Stackoverflow.com/users/59', 'pm_score': 7, 'selected': True, 'text': '<p>I was glad to find <a href="https://web.archive.org/web/20170323034837/http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2004-02/1382.html" rel="noreferrer">a good conversation</a> on this subject, as I hadn\'t really given it much thought before.</p>\n<p>In summary, signed is a good general choice - even when you\'re dead sure all the numbers are positive - if you\'re going to do arithmetic on the variable (like in a typical for loop case).</p>\n<p>unsigned starts to make more sense when:</p>\n<ul>\n<li>You\'re going to do <strong>bitwise things</strong> like masks, or</li>\n<li>You\'re <strong>desperate</strong> to to take advantage of the sign bit <strong>for that extra positive range</strong> .</li>\n</ul>\n<p>Personally, I like signed because I don\'t trust myself to stay consistent and avoid mixing the two types (like the article warns against).</p>\n'}, {'answer_id': 347, 'author': 'helloandre', 'author_id': 50, 'author_profile': 'https://Stackoverflow.com/users/50', 'pm_score': 3, 'selected': False, 'text': "<p>In your example above, when 'i' will always be positive and a higher range would be beneficial, unsigned would be useful. Like if you're using 'declare' statements, such as: </p>\n\n<pre><code>#declare BIT1 (unsigned int 1)\n#declare BIT32 (unsigned int reallybignumber)\n</code></pre>\n\n<p>Especially when these values will never change.</p>\n\n<p>However, if you're doing an accounting program where the people are irresponsible with their money and are constantly in the red, you will most definitely want to use 'signed'.</p>\n\n<p>I do agree with saint though that a good rule of thumb is to use signed, which C actually defaults to, so you're covered.</p>\n"}, {'answer_id': 354, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 3, 'selected': False, 'text': "<p><code>size_t</code> is often a good choice for this, or <code>size_type</code> if you're using an STL class.</p>\n"}, {'answer_id': 1226, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 3, 'selected': False, 'text': '<p>C and C++ compilers will generate a warning when you compare signed and unsigned types; in your example code, you couldn\'t make your loop variable unsigned and have the compiler generate code without warnings (assuming said warnings were turned on).</p>\n\n<p>Naturally, you\'re compiling with warnings turned all the way up, right?</p>\n\n<p>And, have you considered compiling with "treat warnings as errors" to take it that one step further?</p>\n\n<p>The downside with using signed numbers is that there\'s a temptation to overload them so that, for example, the values 0->n are the menu selection, and -1 means nothing\'s selected - rather than creating a class that has two variables, one to indicate if something is selected and another to store what that selection is.  Before you know it, you\'re testing for negative one all over the place and the compiler is complaining about how you\'re wanting to compare the menu selection against the number of menu selections you have - but that\'s dangerous because they\'re different types.  So don\'t do that.</p>\n'}, {'answer_id': 278817, 'author': 'Keith Sirmons', 'author_id': 1048, 'author_profile': 'https://Stackoverflow.com/users/1048', 'pm_score': 3, 'selected': False, 'text': '<p>I would think that if your business case dictates that a negative number is invalid, you would <em>want</em> to have an error shown or thrown.    </p>\n\n<p>With that in mind, I only just recently found out about unsigned integers while working on a project processing data in a binary file and storing the data into a database. I was purposely "corrupting" the binary data, and ended up getting negative values instead of an expected error.  I found that even though the value converted, the value was not valid for my business case.<br>\nMy program did not error, and I ended up getting wrong data into the database. It would have been better if I had used <code>uint</code> and had the program fail.</p>\n'}]
['https://Stackoverflow.com/questions/336', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/61/']