This post was written to elaborate on any curiosities you may have on the differences between signed and unsigned integer variables in C. Read below to find out exactly what they are and why you should be using the both of them.

First and foremost, a signed integer can contain numbers in a range built on the same amount of numbers as that of an unsigned integer’s range, although they are at a different end spectrum.
By creating a variable using the "int" function, you have created a signed integer variable. This default can be reversed by using the "unsigned int" function. Why would you want to do that you ask? Well that is simple:

  • A signed integer variable can contain a numerical data value within the range of -32768 to 32767.
  • An unsigned variable can contain a numerical data value within the range of 0 to 65535.

Now why would that be useful? You may be wondering. It is quite simple. For a vast majority of applications you will use the default signed integer variable type. Which is no less than satisfactory; unless you wish to create a variable that will be used to contain a number larger than 32767 or a variable that will force a positive number.

 

A signed integer variable can be very volatile if used in incorrect situations. Unlike a floating point variable, the signed integer variable will become a negative number if its range is exceeded, if the range is exceeded enough, it may even become positive again. This creates a potential for much inaccuracy. For example, if you were to use the "scanf()" function to allow the user at the keyboard to enter a value for a signed integer and he or she decided to enter the value 32768 for example, which is one number above the allowed maximum for a signed integer variable, then the value of the signed integer variable will become something crazy. In my case it returned "909586995".

 

When using an unsigned integer, you have a higher range in positive numbers, although you have no option for negative numbers. The range for an unsigned integer is, as stated before, 0 to 65535. Which is much more practical for a majority of more technical applications that require avoidance of negative numbers.

 

Now that you have indulged your mind in the wonderful language that is C. Perhaps you would like to head over to the explanation on the different types of “Floating point numbers” linked below:

https://chafflube.wordpress.com/2011/03/20/signed-vs-unsigned-integer-variables-in-c/

I do so hope that this enhanced your knowledge on the use of signed and unsigned integer variables in the C language. Enjoy your newfound C integer variable knowledge.

Advertisements