Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   signed and unsigned char (http://www.velocityreviews.com/forums/t440532-signed-and-unsigned-char.html)

Joseph Wakeling 12-14-2005 07:27 AM

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 12-14-2005 07:43 AM

Re: signed and unsigned char
 
On 2005-12-14, Joseph Wakeling <joseph.wakeling@gmail.com> 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 *

Joseph Wakeling 12-14-2005 08:11 AM

Re: signed and unsigned char
 
Thanks very much! :-)


Gerr 12-14-2005 08:15 AM

Re: signed and unsigned char
 
> > 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 ?


Jordan Abel 12-14-2005 09:09 AM

Re: signed and unsigned char
 
On 2005-12-14, Gerr <gertvierman@hotmail.com> 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 *.


All times are GMT. The time now is 06:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.