int vs unsigned int

Hi! When should I use unsigned int instead of int?

Should I always use it when I have a loop like this:

for (unsigned int i = 0; i < SIZE, i++)
{ some code }

Are there other cases I should use unsigned instead of just int?
Whenever you have a value that can only be positive IMO.
You don't have to do it like you said in all your for loops as well - I guess it just adds some extra typing to the for loop. If you know what your outcome is going to be - then it should be fine.

But if your passing something like
unsigned int SIZE;
into the for loop - then you should use - for( unsigned int i = 0; i < SIZE ; ++i )
You only need it in a for loop like that if size is going to be larger than 2^31 (two billion). Using unsigned gives 2^32.
unsigned allows for a number twice as large at the cost of negatives.
In the case of loops, it is better to make sure that i is of the same type as SIZE. In the case of std containers use the appropriate size_type defined by the container rather than int or unsigned int.
size_t is a typedef of an unsigned type
Topic archived. No new replies allowed.