Signed vs. Unsigned Integers Explained

This article explains the difference between signed and unsigned integers.

Let’s illustrate the difference using a short integer, which typically occupies 2 bytes of storage in the ‘C’ programming language.

  • Signed Integer: In this case, a signed integer can represent values ranging from -32768 to +32767. It can store both positive and negative numbers.

  • Unsigned Integer: An unsigned integer, on the other hand, will have a range from 0 to 65535. It can only store positive values in the variable.

In the printf statement:

  • %d is used to print a signed integer.
  • %u is used to print an unsigned integer.

Example

Let’s say you have a variable intended to store the age of a person. Age will never be negative, so you might choose to use an unsigned integer to represent it. If, however, you were tracking temperature changes that could go below zero, a signed integer would be necessary.