C/C++ use of int or unsigned int -


in lot of code examples, source code, libraries etc. see use of int when far can see, unsigned int make more sense.

one place see lot in for loops. see below example:

for(int = 0; < length; i++) {     // stuff } 

why on earth use int rather unsigned int? laziness - people can't bothered typing unsigned?

using unsigned can introduce programming errors hard spot, , it's better use signed int avoid them. 1 example when decide iterate backwards rather forwards , write this:

for (unsigned = 5; >= 0; i--) {     printf("%d\n", i); } 

another if math inside loop:

for (unsigned = 0; < 10; i++) {     (unsigned j = 0; j < 10; j++) {         if (i - j >= 4) printf("%d %d\n", i, j);     } } 

using unsigned introduces potential these sorts of bugs, , there's not upside.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -