Thread: Is char unsigned by default?

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Is char unsigned by default?

    I've seen a lot of "unsigned char" in the past, but is it already unsigned by default or is it implementation specific?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is implementation specific.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Usually I think it is signed by default, however, which is why you will see "unsigned char" used in a lot of code.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is implementation specific as previously mentioned.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Usually I think it is signed by default, however, which is why you will see "unsigned char" used in a lot of code.
    I think that the real reason 'why you will see "unsigned char" used in a lot of code' is that the code involved requires the use of unsigned integers for correctness, e.g., bit shifting. Thus, it makes sense to specify unsigned char rather than assuming that the implementation will have char as unsigned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Ah, so I guess the moral of the story is specify it anyway if it's important - even if it's just being clear to other developers that you intend this to be unsigned.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    char is always unsigned. for example if u give char a=-257 it wont bother about '-'. it will simply print value of corresponding to char 1, some special symbol.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by magestrium
    char is always unsigned.
    Sorry, but the 1999 edition of the C standard disagrees with you, so you must be wrong:
    Quote Originally Posted by C99 Section 6.2.5 Paragraph 15 and note 35
    The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

    CHAR_MIN, defined in <limits.h>, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.
    Quote Originally Posted by magestrium
    for example if u give char a=-257 it wont bother about '-'. it will simply print value of corresponding to char 1, some special symbol.
    Your example has to do with how the value is printed, not with whether char is signed or unsigned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Quote Originally Posted by magestrium View Post
    char is always unsigned
    Sorry, but the 1999 edition of the C standard disagrees with you, so you must be wrong:
    Just to expand on that: the 1989 C standard and the C++ standard also specify (albeit in slightly different prose) that it is implementation defined whether char is unsigned or signed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Results from various porting platforms

    Wotcher,

    I needed to know, so I wrote a quick program and tested it on our various porting platforms. The following platforms all have char == signed char:
    Windoze, Solaris(sparc), Solaris(intel), HP(risc), HP(intel), Linux(intel).

    IBM, however, buck the tend. All these platforms have char == unsigned char:
    Aix, Linux(s390), Linux(PowerPC).

    Hope that helps,

    Grelly

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Actually it's configurable in Visual Studio IIRC.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Microsoft compilers have a flag to change it
    /J (Default char Type Is unsigned) (C++)

    GCC compilers have a flag to change it
    Options Controlling C Dialect

    If you really care about the signed-ness of your chars, then just say "signed char" or "unsigned char" and forget about whether your current compiler has a flag to fix it for you.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    I can't help feeling that to use compiler options to change a default behaviour is a bad idea. It won't be obvious to the next developer that the sign-ness has been reversed.

    My recommendation is to get into the habit of using the signed/unsigned qualifiers the whole time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM