signed and unsigned char

Discussion in 'C Programming' started by Joseph Wakeling, Dec 14, 2005.

  1. Hello all,

    Is there any particular advantage or reason for always declaring a char
    to be signed or unsigned?

    Further, when compiling with gcc on Linux, I received warnings when
    trying to sprintf an unsigned char *. I presume this is because in
    Linux the default char type is signed. Is there any way to avoid the
    error? Or should I not be using unsigned chars where the result would
    be used with printf, sprintf, etc.?

    Many thanks,

    -- Joe
    Joseph Wakeling, Dec 14, 2005
    #1
    1. Advertising

  2. Joseph Wakeling

    Jordan Abel Guest

    On 2005-12-14, Joseph Wakeling <> wrote:
    > Hello all,
    >
    > Is there any particular advantage or reason for always declaring a char
    > to be signed or unsigned?
    >
    > Further, when compiling with gcc on Linux, I received warnings when
    > trying to sprintf an unsigned char *. I presume this is because in
    > Linux the default char type is signed. Is there any way to avoid the
    > error? Or should I not be using unsigned chars where the result would
    > be used with printf, sprintf, etc.?


    In general, strings should just be char * - if you have an individual
    char variable, pick signed or unsigned based on what you want it to be.
    If you have a pointer that points at a block of bytes [some raw data for
    something], it should be unsigned char *
    Jordan Abel, Dec 14, 2005
    #2
    1. Advertising

  3. Thanks very much! :)
    Joseph Wakeling, Dec 14, 2005
    #3
  4. Joseph Wakeling

    Gerr Guest

    > > Or should I not be using unsigned chars where the result would
    > > be used with printf, sprintf, etc.?

    >
    > In general, strings should just be char * - if you have an individual
    > char variable, pick signed or unsigned based on what you want it to be.
    > If you have a pointer that points at a block of bytes [some raw data for
    > something], it should be unsigned char *


    What about strings using some extended character encoding, iso-8859-1
    for example; are those to be considered as binary data (unsigned char
    *) or regular
    strings (char *) ? Is printf()'ing these strings legal ?
    Gerr, Dec 14, 2005
    #4
  5. Joseph Wakeling

    Jordan Abel Guest

    On 2005-12-14, Gerr <> wrote:
    >> > Or should I not be using unsigned chars where the result would
    >> > be used with printf, sprintf, etc.?

    >>
    >> In general, strings should just be char * - if you have an individual
    >> char variable, pick signed or unsigned based on what you want it to be.
    >> If you have a pointer that points at a block of bytes [some raw data for
    >> something], it should be unsigned char *

    >
    > What about strings using some extended character encoding, iso-8859-1
    > for example; are those to be considered as binary data (unsigned char
    > *) or regular
    > strings (char *) ? Is printf()'ing these strings legal ?


    read your implementation docs.

    but consider that "strings" i.e. string literals have type char *.
    Jordan Abel, Dec 14, 2005
    #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,761
    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:
    579
    Jack Klein
    May 9, 2005
  3. At_sea_with_C

    char vs. signed or unsigned char

    At_sea_with_C, Mar 14, 2007, in forum: C Programming
    Replies:
    4
    Views:
    752
    Malcolm McLean
    Mar 14, 2007
  4. Ioannis Vranos
    Replies:
    11
    Views:
    758
    Ioannis Vranos
    Mar 28, 2008
  5. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    611
    Ben Bacarisse
    Mar 29, 2008
Loading...

Share This Page