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
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
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
Do you know how a binary number is written?
For simplicity, I'll stick to a four digit binary number
1010 -> This means 1*2^{3} + 0*2^{2} + 1*2^{1} + 0*2^{0 }Or more generally abcd ->a*2^{3} + b*2^{2} + c*2^{1} + d*2^{0}
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
Fact - Beethoven wrote his first symphony in C
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?
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; }
Fact - Beethoven wrote his first symphony in C
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.