internal representation of signed/unsigned chars, ints

This is a discussion on internal representation of signed/unsigned chars, ints within the C Programming forums, part of the General Programming Boards category; If I run the following program: Code: #include <stdio.h> #include <stdlib.h> int main() { unsigned char c = -1; unsigned ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    internal representation of signed/unsigned chars, ints

    If I run the following program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned char c = -1;
        unsigned i = -1;
    
        printf("c: %d\n", c);
        printf("i: %d\n", i);
    
        return 0;
    }
    I get as output:

    c: 255
    i: -1

    I am assuming that on my machine (Pentium 4) a twos complement representation is used.

    An int is 4 bytes long, so I presume the constant -1 is represented internally as a signed integer 11111111 11111111 11111111 11111111.

    I also presume that when the unsigned char c is set equal to -1, the eight bits of c are set equal to the 8 lowest order bits of the above signed integer constant, i.e. 11111111.

    This would be consistent with the value of 255 output for c.

    So what is going on with i????? I would have expected the same logic to apply and a value of 65535 to be output. Even if the above assumptions are incorrect (in particular I am not certain if -1 is stored as an signed int and whether it is true that a signed int is converted to an unsigned char simply by discarding the 3 highest order bytes), I can't see why printf should output a value of -1 for a variable that has been defined as unsigned.

    An suggestions would be most appreciated.

    Incidentally, if I try the same thing in C++

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        unsigned i = -1;
        cout << "i: " << i << endl;
        
        return 0;
    }
    I get the output:

    i: 4294967295

    which is FFFFFFFF, or eight bytes with all bits set to 1. How you get that from a 4-byte unsigned int I cannot imagine.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you think that [the code within printf that handles] %d knows if the number is unsigned or signed? Or perhaps that's why there is a %u to tell printf that it is an unsigned number?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Whoops (blushes). Slight case of not seeing the wood for the trees. And FFFFFFFF is of course four bytes with all bits set to 1...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Carrying ints a chars over to other functions.
    By jmajeremy in forum C++ Programming
    Replies: 39
    Last Post: 04-04-2007, 07:17 PM
  3. atoi not converting chars to ints?
    By C++Child in forum C++ Programming
    Replies: 13
    Last Post: 10-08-2004, 04:59 PM
  4. Chars - ints
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 09:22 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 12:33 PM

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