signed and unsigned char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph Wakeling

    #1

    signed and unsigned char

    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

  • Jordan Abel

    #2
    Re: signed and unsigned char

    On 2005-12-14, Joseph Wakeling <joseph.wakelin g@gmail.com> wrote:[color=blue]
    > 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.?[/color]

    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 *

    Comment

    • Joseph Wakeling

      #3
      Re: signed and unsigned char

      Thanks very much! :-)

      Comment

      • Gerr

        #4
        Re: signed and unsigned char

        > > Or should I not be using unsigned chars where the result would[color=blue][color=green]
        > > be used with printf, sprintf, etc.?[/color]
        >
        > 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 *[/color]

        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 ?

        Comment

        • Jordan Abel

          #5
          Re: signed and unsigned char

          On 2005-12-14, Gerr <gertvierman@ho tmail.com> wrote:[color=blue][color=green][color=darkred]
          >> > Or should I not be using unsigned chars where the result would
          >> > be used with printf, sprintf, etc.?[/color]
          >>
          >> 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 *[/color]
          >
          > 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 ?[/color]

          read your implementation docs.

          but consider that "strings" i.e. string literals have type char *.

          Comment

          Working...