Thread: On the sign of char

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    On the sign of char

    I quite didn't catch yet how char is represented regarding its sign. What I got so far is that char, much like any other integral, can be sign or unsigned. But unlike other integrals the decision of which is the default representation is left to the compiler. So far so good.

    However, I read repeatedly that there's a third type of char; a plain char. And that this type cannot be represented. However I wonder why it is mentioned and what does it actually mean?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    signed char a;
    This is signed

    unsigned char b;
    This is unsigned

    char c;
    This is implementation specific as to whether it is signed or unsigned.
    For representing the printable ASCII characters, it doesn't matter.

    For anything else, it is best to be specific.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    So the 'plain char' term is only used to identify an implementation specific char? Sounds strange considering that the same treatment is not given to other integrals... I suppose because they are not implementation defined?

    This is the quote from C++ Primer:
    Unlike the other integral types, there are three distinct types for char: plain char, signed char, and unsigned char. Although there are three distinct types, there are only two ways a char can be represented. The char type is respresented using either the signed char or unsigned char version. Which representation is used for char varies by compiler.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char is always equivalent to "unsigned char" or "signed char".
    The implementation specific behaviour is which one you get.

    gcc for example has compiler switchs
    -fsigned-char
    -funsigned-char
    to specifically request which version you end up with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ok. I think I got it through my thick skull.

    A plain char is regarded as a separate type from unsigned int and signed int, only in the context of it being implementation defined. The compiler understands three types; unsigned char, signed char and plain char, being that a plain char will be one of unsigned or signed.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 11:49 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM