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

c# - Sort XmlNodeList with a specific Node value -

Android Java.Lang.RuntimeException : Unable to start activity Component Info -

actionscript 3 - Equivalent to moveReceived in ElectroServer 5 -