Like Tree3Likes
  • 2 Post By std10093
  • 1 Post By whiteflags

Difference between signed and unsigned integers

This is a discussion on Difference between signed and unsigned integers within the C Programming forums, part of the General Programming Boards category; Hi guys, I am new to C programming and wanted to know what the key differences are between signed and ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    Question 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. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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
    Messi 10 and umutefiloglu like this.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    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
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    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. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    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

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    Quote Originally Posted by Messi 10 View Post
    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:
    Quote 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.
    Click_here likes this.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    834
    Quote Originally Posted by Messi 10 View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned vs. Signed?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2009, 12:15 PM
  2. Signed and unsigned
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 09-18-2007, 12:27 PM
  3. signed or unsigned?
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 05-08-2007, 01:06 AM
  4. Difference between unsigned and signed numbers...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-26-2002, 08:48 AM
  5. DIfference between signed and unsigned integers ?
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 01-15-2002, 08:27 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21