Is char unsigned by default?

This is a discussion on Is char unsigned by default? within the C Programming forums, part of the General Programming Boards category; I've seen a lot of "unsigned char" in the past, but is it already unsigned by default or is it ...

  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,185
    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
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    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%.

  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,308
    Actually it's configurable in Visual Studio IIRC.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04:04 AM
  4. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 03:38 PM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM

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