char vs. signed or unsigned char

Discussion in 'C Programming' started by At_sea_with_C, Mar 14, 2007.

  1. hello all,

    Is there a reason to prefer char over signed or unsigned char. From waht I
    know all strings can be coded with unsigned char while a negative number
    can be put in signed char. So what are the uses for plain char?

    Thanks to all who ansered my last post (enum).

    --
    Email: The handle, (dot seperated), at gmail dot com.
    At_sea_with_C, Mar 14, 2007
    #1
    1. Advertising

  2. At_sea_with_C <> wrote:
    > Is there a reason to prefer char over signed or unsigned char. From waht I
    > know all strings can be coded with unsigned char while a negative number
    > can be put in signed char. So what are the uses for plain char?


    As far as I know, there is no "plain char". A char is either a unsigned
    char or a signed char, which exactly is implementation depenedent:

    ISO/IEC 9899:1999, 6.2.5.15 (p. 49)
    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.


    HTH,
    Flo
    Florian Weingarten, Mar 14, 2007
    #2
    1. Advertising

  3. At_sea_with_C

    Chris Dollin Guest

    Florian Weingarten wrote:

    > At_sea_with_C <> wrote:
    >> Is there a reason to prefer char over signed or unsigned char. From waht I
    >> know all strings can be coded with unsigned char while a negative number
    >> can be put in signed char. So what are the uses for plain char?

    >
    > As far as I know, there is no "plain char".


    `char` is plain.

    > A char is either a unsigned char or a signed char, which exactly
    > is implementation depenedent:
    >
    > ISO/IEC 9899:1999, 6.2.5.15 (p. 49)
    > 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.


    No: it has the same range, representation, and behaviour as one of
    those types, but it's a different type.

    I'd expect the implementation to pick whichever one was "more
    efficient" by some reasonable measure - eg when converting to
    `int`, one probably wants an efficient code sequence, so one
    picks whatever the machine conversion is (PDP11 MOVB, for example)
    and takes the consequences.

    --
    Chris "electric hedgehog" Dollin
    "We live for the One, you die for the One." Unsaid /Babylon 5/.
    Chris Dollin, Mar 14, 2007
    #3
  4. At_sea_with_C wrote:

    > Is there a reason to prefer char over signed or unsigned char. From waht I
    > know all strings can be coded with unsigned char while a negative number
    > can be put in signed char. So what are the uses for plain char?


    use char for character data (supported by the implementation)

    use unsigned char for raw data, eg. data transmitted over a comms link

    I've never used signed char, I suppose you'd use it for small signed
    integers


    --
    Nick Keighley
    Nick Keighley, Mar 14, 2007
    #4
  5. "Nick Keighley" <> wrote in message
    >
    > I've never used signed char, I suppose you'd use it for small signed
    > integers
    >

    One use is for normals in runtime graphics.
    Three numbers in the range -128 to 127 give you enough resolution to do
    visually acceptable lighting, but don't take up too much space, and you can
    use fast integer arithmetic.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    1,736
    Peter Nilsson
    Jul 26, 2004
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    570
    Jack Klein
    May 9, 2005
  3. Ioannis Vranos
    Replies:
    11
    Views:
    745
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    601
    Ben Bacarisse
    Mar 29, 2008
  5. Alex Vinokur
    Replies:
    9
    Views:
    770
    James Kanze
    Oct 13, 2008
Loading...

Share This Page