char type and bits

This is a discussion on char type and bits within the C Programming forums, part of the General Programming Boards category; Hello everybody! I have a question about chars in C. I know that chars are integer represented with 8 bits ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    char type and bits

    Hello everybody!
    I have a question about chars in C. I know that chars are integer represented with 8 bits (1 byte). So, we can represent numbers from 0 up to 255.
    If in fact we write a function printbits(char c) and call printbits(255) we get 11111111.
    in the same way, 87=01010111and 88= 01011000. if we then write this code:
    Code:
    	char a= 87,b=88;
    	char c=a+b;
           if (c>100) printf("OK");
    OK never gets written. the reason is that 87+88 evaluates to 10101111 and is automatically interpreted as -81, a negative number. This means that the result is interpreted in 2's complement.
    Actually, if we say that chars are integers, this would make sense. But what i cannot understand is why the 255 is evaluated correctly then..? Is it casted to something like an unsigned?
    thanks for lighting my mind!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because 255 is ALSO -1, which is 11111111 in an 8-bit integer. If you do:
    Code:
    char c = 255;
    int x = c;
    printf("%d\n", x);
    then I expect you'll see that it comes out as -1.

    --
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That depends entirely on how you defined printbits. If printbits is defined as taking a char value, then 255 is converted to a char value (and therefore becomes -1, which has the bit pattern 11111111, which is what you wanted anyway).

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I know that chars are integer represented with 8 bits (1 byte).
    A byte isn't required to be 8 bits, but yes, "char" and "byte" are synonymous in C.

    >But what i cannot understand is why the 255 is evaluated correctly then..?
    Probably because printbits is written in such a way that the signedness of the type is irrelevant. You always end up with the bit pattern, which doesn't change.
    My best code is written with the delete key.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Change it to this and it will work:
    Code:
    char a= 87,b=88;
    unsigned char c=a+b;
    if (c>100) printf("OK");
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by Dino View Post
    Change it to this and it will work:
    Code:
    char a= 87,b=88;
    unsigned char c=a+b;
    if (c>100) printf("OK");
    i read that in some systems char is unsigned by default, while in other it's signed..
    so it seems that in linux (my system) it's signed by default...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    a+b would still be an overflow if char is signed.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by smoking81 View Post
    i read that in some systems char is unsigned by default, while in other it's signed..
    so it seems that in linux (my system) it's signed by default...
    It actually depends more on the compiler than the OS. There is nothing easier or harder in dealing with signed verses unsigned char from an OS or processor point of view, given current technology.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by smoking81 View Post
    i read that in some systems char is unsigned by default, while in other it's signed..
    I've heard that too, but does anyone know which platforms are unsigned by default?
    Wouldn't that break a lot of code, since most people don't bother explicitly writing signed char, they just write char...

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by cpjust View Post
    I've heard that too, but does anyone know which platforms are unsigned by default?
    Wouldn't that break a lot of code, since most people don't bother explicitly writing signed char, they just write char...
    I quote this question: it would be very useful to know that! bye

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by cpjust View Post
    I've heard that too, but does anyone know which platforms are unsigned by default?
    Wouldn't that break a lot of code, since most people don't bother explicitly writing signed char, they just write char...
    Not really, most of the time when you use char, you intend on putting an ASCII character into it. printf functions will always correctly decode these as unsigned chars when printing out for example. The trouble only comes when you use char for anything else (i.e. as a byte storage). Just like you would do with shorts and ints, you should then explicitly set it to unsigned if you don't want it to hold negative numbers.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by QuantumPete View Post
    Not really, most of the time when you use char, you intend on putting an ASCII character into it. printf functions will always correctly decode these as unsigned chars when printing out for example. The trouble only comes when you use char for anything else (i.e. as a byte storage). Just like you would do with shorts and ints, you should then explicitly set it to unsigned if you don't want it to hold negative numbers.

    QuantumPete
    But then you should also explicitly set it to signed if you DO want it to hold negative numbers, right?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Dino says declare what you want.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by QuantumPete View Post
    Not really, most of the time when you use char, you intend on putting an ASCII character into it. printf functions will always correctly decode these as unsigned chars when printing out for example.
    Actually, printf decodes these as ints. But only the last 7 or 8 bits are relevant to printing a character symbol, and these are the same even if the argument is sign extended.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by King Mir View Post
    Actually, printf decodes these as ints. But only the last 7 or 8 bits are relevant to printing a character symbol, and these are the same even if the argument is sign extended.
    I should have said "treat these" instead of "decodes these". Sorry about the confusion!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Playing card data structure?
    By crypticgeek in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2006, 04:29 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  4. Replies: 10
    Last Post: 06-26-2005, 11:27 AM
  5. Cross platform portability, about data types...
    By gaah in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2005, 09:32 PM

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