# Thread: Difference between signed and unsigned integers

1. ## Difference between signed and unsigned integers

Hi guys,

I am new to C programming and wanted to know what the key differences are between signed and unsigned integers. Also, what is the difference between "%u" and "%d"?

Thanks

2. Hello Lionel, can you give me an autograph? No I am kidding I already have one :P

Signed integers, are the signed numbers. Negative ones, zero and positive numbers.

Unsigned integers are the zero and the positive numbers.

%d is the proper specifier for signed integers

%u is the proper specifier for unsigned integer

I think that these are enough for a start

3. Do you know how a binary number is written?

For simplicity, I'll stick to a four digit binary number

1010 -> This means 1*23 + 0*22 + 1*21 + 0*20

Or more generally abcd ->a*23 + b*22 + c*21 + d*20

So this four digit number can represent 0 to 15.

How (you might ask) would you represent, say, -1?

There are a few different representations, but you will usually see 2's complement - It is usually explained by a clock representation
(here), -1 being the first number anticlockwise to the 0 (-1 = 1111)

The number range then becomes -8 to 7

Signed number representations - Wikipedia, the free encyclopedia

4. Thanks for the explanation! Could you also state an example as to when we would need to use an unsigned integer rather than a signed integer?

5. Basically, you need to be aware of what your number range is going to be - Unsigned is for something that is going to be large and will never be negative, or used with something that will be negative. For example "If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?" (From projecteuler)

Try not to mix an match though, as this results in false on my compiler due to integer promotion - A terrible bug to have to find.

Code:
```int main(void)
{
if ( -1 < (unsigned int) 1)
{
printf("-1 is less than (unsigned int) 1");
}
else
{
printf("-1 NOT less than (unsigned int) 1");
}

return EXIT_SUCCESS;
}```

6. Originally Posted by Messi 10
Thanks for the explanation! Could you also state an example as to when we would need to use an unsigned integer rather than a signed integer?
Unsigned variables are for working with bits. It's much simpler. Take for example what should happen when you shift:
Originally Posted by 6.5.7 C99
4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with
zeros. If E1 has an unsigned type, the value of the result is E1 * 2^(E2), reduced modulo
one more than the maximum value representable in the result type. If E1 has a signed
type and nonnegative value, and E1 * 2^(E2) is representable in the result type, then that is
the resulting value; otherwise, the behavior is undefined.

5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type
or if E1 has a signed type and a nonnegative value, the value of the result is the integral
part of the quotient of E1 / 2^(E2). If E1 has a signed type and a negative value, the
resulting value is implementation-defined.

7. Originally Posted by Messi 10
when we would need to use an unsigned integer rather than a signed integer?
There are a number of special types, that are actually either signed or unsigned integers (unsigned or signed short, int or long types), that one should use in different situations.

For example, to indicate an amount of memory -- say, the length of a string, or the number of ints in an array, you should use size_t. If you look at how e.g. strlen() is defined, it actually returns a nonnegative integer of type size_t, not an int.

If you want a 64-bit signed integer, use int64_t (defined in stdint.h). Same for other integer types of specific size (or minimum size) and signedness.

If you want to manipulate as large integers as the current architecture can natively handle, use long or unsigned long, depending on whether you'll need negative numbers or not. (Many compilers do support long long, which may be even longer, though.) If you don't need negative numbers, the unsigned type can hold a larger value (on current architectures, twice the value the signed type can hold, plus one).

If you want to know how large values each type can represent, include limits.h, and look at INT_MIN, INT_MAX, UINT_MAX, and so on.

Personally, I tend use the special types (mainly size_t and ssize_t) when appropriate (look at the functions you use, and what type the parameters are), specific-sized integers for binary storage (files) and functions like random number generators which are sensitive to the range of possible values, unsigned integer types for counts, and signed integers for integer data.