range of unsigned integer

This is a discussion on range of unsigned integer within the C Programming forums, part of the General Programming Boards category; Hello friends, I am following a book that states that range of unsigned integers is from 0 to 65535 for ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    range of unsigned integer

    Hello friends,

    I am following a book that states that range of unsigned integers is from 0 to 65535 for 16-bit compilers but this simple program has perplexed me:

    Code:
    main()
    {
       unsigned i, j;
       clrscr();
       i = 32768;
       j = 32767;
       printf("%d      %d",i,j);
       getch();
    }
    The output given by this program is -32768 32767.
    Why is it going to the negative side when the range is from 0 to 65535;
    I am using Borland C++ version 3.1 in Windows XP.
    Thanks a lot.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    printf doesnt know the type of value you pass ( how should it, you can pass any type you want ).
    It trusts on the format spacifiers that you pass in the format string.
    You passed %d that stands for a signed integer.
    Kurt

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, using a 16-bit compiler on a 32-bit OS at the present time is pretty dumb. You'll learn a bunch of stuff that is already outdated currently, but will be less and less useful as 64-bit systems are being rolled out.

    Secondly, signed and unsigned is really more of a concept. You're not making the variable itself signed or unsigned. You're giving a hint to the compiler how to actually treat math operations and such. Consider binary representation for a second.

    If I gave the 16-bit number 0xFFFF, what number is it? The signed representation could be -1 while the unsigned representation could be 65535. printf() doesn't have the luxury of knowing what type of number you're giving it because it's a variadic function. It acts on the arguments it receives based upon what is in the first argument. This is why you're using things like %d. %d tells printf() to treat a variable as if it's an integer. The issue is that printf() will treat the value matching %d as if it's signed.

    Hint: Try %u.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 01:52 AM
  3. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM

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